Forum Moderators: phranque
This is what I would like to do to site example.com using .htaccess
There will be about 5 categories that I would like to look like so:
http://www.example.com/about
http://www.example.com/news
http://www.example.com/contact
http://www.example.com/history
http://www.example.com/profile
I'd like to use the about, news, contact, history and profile as $_GET variables to retrieve info from DB.
and on top of that, within profile and news, I'd like to add a further depth so that pages within those categories would be:
http://www.example.com/news/story1
http://www.example.com/news/story2
http://www.example.com/news/storyadnauseum
http://www.example.com/profile/profile1
http://www.example.com/profile/profile2
http://www.example.com/profile/profileadnauseum
I'd like to use the additional depths as $_GET variables to also retrieve info from my DB.
Please could you help me out as I am pretty much useless in .htaccess and the tutorials I've read aren't helping me get very far.
Thanks!
# Internally rewrite search engine friendly static URL to dynamic filepath and query
RewriteRule ^index.php/([^/]+)/?$ /index.php?page=$1 [L]
RewriteRule ^index.php/([^/]+)/([^/]+)/?$ /index.php?page=$1&subpage=$2 [L]
#
# Externally redirect client requests for old dynamic URLs to equivalent new static URLs
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=([^\ ]+)\ HTTP/
RewriteRule ^index\.php$ http://example.localhost/%1? [R=301,L]
# Externally redirect client requests for old dynamic URLs to equivalent new static URLs
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=([^&]+)&subpage=([^\ ]+)\ HTTP/
RewriteRule ^index\.php$ http://example.localhost/%1/%2? [R=301,L]
You will need to reverse the last two rules -- Always put your rules in most-specific to least-specific order, or you may get unexpected results.
According to this rule-of-thumb, the first two rules should also be reversed, but as their patterns are more exclusive, it doesn't actually matter.
An alternative would be to change the pattern in the third rule to "^[A-Z]{3,9}\ /index\.php\?page=([^&\ ]+)\ HTTP/ "
It is also a good rule of thumb to always place external redirects before internal rewrites.
Jim