Forum Moderators: phranque

Message Too Old, No Replies

Another boring .htaccess request

         

Pico_Train

7:39 am on Jul 6, 2008 (gmt 0)

10+ Year Member




Hi there everybody!

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!

Pico_Train

11:26 am on Jul 7, 2008 (gmt 0)

10+ Year Member



Ok this is what I have got after trying JD Morgan's tutorial and now I'm stuck...been two hours staring at this...

# 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]

jdMorgan

3:21 pm on Jul 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What seems to be the problem? I'm pretty much useless at mind-reading... :)

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

Pico_Train

4:42 pm on Jul 7, 2008 (gmt 0)

10+ Year Member



Ok, great thanks JD!

After the better part of the day messing around and particularly after reading your post, I got it right.

I think I learnt a thing or two today!

Thanks for your help and your valued participation in these forums.

g1smd

8:32 pm on Jul 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Don't forget to put all the redirects before all of the rewrites too.

The redirects are the things with the [R=301] within.