htaccess Redirect - First Segment to PHP File, Second Segment as Parameter
Posted
by
Steve
on Stack Overflow
See other posts from Stack Overflow
or by Steve
Published on 2013-07-02T17:03:07Z
Indexed on
2013/07/02
17:05 UTC
Read the original article
Hit count: 263
My htaccess redirect knowledge is somewhat weak, so I was hoping to get some help here.
I currently have the following redirect, which works well:
# remove trailing slash
RewriteRule ^(.*)/$ /$1 [L,R=301]
# redirect to clean URL
RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-\s]+)$ /$1.php
This takes a URL like www.mysite.com/about
to www.mysite.com/about.php
Now I would like to keep this behavior, but add parameters (if applicable), as such:
www.mysite.com/about
=> www.mysite.com/about.php
www.mysite.com/gallery/1
=> www.mysite.com/gallery.php?id=1
If possible, I might like to expand this system to 2 or more parameters, as such:
www.mysite.com/gallery/1/2
=> www.mysite.com/gallery.php?id=1§ion=2
So the pattern would be:
- First URL segment redirects to a PHP file
- (Optionally) the second segment gets added as the
id
parameter - (Optionally) the third segment gets added as the
section
parameter
© Stack Overflow or respective owner