Deleted .htaccess with simple mod_rewrite - don't know regex

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Deleted .htaccess with simple mod_rewrite - don't know regex

Post by omniuni »

Hello, all.

So, the regex I had, told mod_rewrite to extract the stuff after / and put it after ?page=

In otherwords:

http://example.com/contact would be translated to http://example.com/?page=contact

If someone could help me re-create this single mod-rewrite rule, I would be very grateful.

If you are feeling adventurous, is there a way to get mod_rewrite to do something where

http://example.com/contact/form/help (etc) would translate to http://example.com/?var1=contact&var2=form&var3=help (etc) where var(n) has (n) replaced with the number of the match?

Darn it, I really need to learn regex. :oops:
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: Deleted .htaccess with simple mod_rewrite - don't know regex

Post by ridgerunner »

omniuni wrote:Hello, all.

So, the regex I had, told mod_rewrite to extract the stuff after / and put it after ?page=

In otherwords:

http://example.com/contact would be translated to http://example.com/?page=contact

If someone could help me re-create this single mod-rewrite rule, I would be very grateful.
The re-written target URL should specify the name of a script file that will handle the query parameters - e.g. index.php?page=contact. Assuming your script file is index.php in the root directory, try this:

Code: Select all

RewriteEngine on
 
# if the requested URL is not a real file...
# and the requested URL is not a real directory...
# then capture the URL and rewrite it.
# Note: URL must be exactly one path level deep
# Note: URL may have optional trailing / which is stripped off
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?page=$1
omniuni wrote:If you are feeling adventurous, is there a way to get mod_rewrite to do something where

http://example.com/contact/form/help (etc) would translate to http://example.com/?var1=contact&var2=form&var3=help (etc) where var(n) has (n) replaced with the number of the match?
Yes and no. You cannot write one rule to handle arbitrary depths (passing each directory in a separate query variable), but you can write one rule for each path depth that needs to be handled. (URLs having longer paths will get an Apache "Not Found" page.) The following .htaccess handles three levels deep:

Code: Select all

RewriteEngine on
 
# one level deep
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?var1=$1
 
# two levels deep
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?var1=$1&var2=$2
 
# three levels deep
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?var1=$1&var2=$2&var3=$3
Alternatively, you could capture the whole path string (having arbitrary depth) and re-write it into one query variable, and then parse the string in your script file. Here is a .htaccess file that does this:

Code: Select all

RewriteEngine on
 
# pass multi-level path in one query variable
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/?$ index.php?multipath=$1
omniuni wrote:Darn it, I really need to learn regex. :oops:
Yes, but beware... they can become addicting!

Hope this helps :)
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Deleted .htaccess with simple mod_rewrite - don't know regex

Post by omniuni »

HOPE it HELPS!?

More like IT SAVES MY LIFE!!!!! THANK YOU!

You have absolutely made things 100x easier, and I see how all that works! Thank you again!

Now, I do have one slight question, since you give me that multipath code...

How would I add (if possible) a clause that if there are 3 or less parts of the path, make var1, var2, var3 (as you showed), and if there are more, pass it all to var (not var1) with "multipath=true" at the end.

In other words: http://example.com/contact/omniuni

would become http://example.com/?var1=contact&var2=omniuni

but http://example.com/contact/omniuni/isreally/annoying

would become http://example.com/?var=contact/omniuni/isreally/annoying&multipath=true

I really really appreciate your help with this.
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: Deleted .htaccess with simple mod_rewrite - don't know regex

Post by ridgerunner »

omniuni wrote:... How would I ...
Well, like so...

Code: Select all

RewriteEngine on
 
# one level deep
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?var1=$1
 
# two levels deep
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?var1=$1&var2=$2
 
# three levels deep
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?var1=$1&var2=$2&var3=$3
 
# more than 3 deep? then pass whole mess in var and add multipath=true
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+/[^/]+/[^/]+/.+)$ index.php?var=$1&multipath=true
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Deleted .htaccess with simple mod_rewrite - don't know regex

Post by omniuni »

Awesome. Just... Awesome. Thanks again!
Post Reply