Forum Moderators: phranque

Message Too Old, No Replies

non www redirect

redirect a non www page to a www one

         

flapjack

10:13 pm on Nov 19, 2009 (gmt 0)

10+ Year Member



Hi

I have working user friendly earls in place instead of dynamic urls, such as

[mysite.com...]

when I remove the www portion it gives me

[mysite.com...]

instead of taking me to

[mysite.com...]

I've been scratching what little hair I have left in trying to figure out what's wrong with the code but have come up firing blanks. I'd appreciate any thoughts on where I've gone wrong. thanks

category is an alpha only value taken from mysql

here's my original htaccess code

RewriteRule ^\-.html index.php
RewriteRule ^\dummy.html index.php

#redirect non www to www url

RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ [mysite.com...] [R=301]

# rewrite user friendly url from dynamic url
RewriteRule ^([^-]*)-category$ /index.php?cat=$1 [L]

#rewrite requests to index.php to [mysite.com...]
RewriteCond %{THE_REQUEST} ^[A-Za-z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ [mysite.com...] [R=301]

# redirect old dynamic urls to new friendly urls
RewriteCond %{THE_REQUEST} ^[A-Za-z]{3,9}\ /index\.php\?cat=([^\ ]*)\ HTTP/
RewriteRule ^index\.php$ [mysite.com...] [R=301]

g1smd

12:59 am on Nov 20, 2009 (gmt 0)

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



Rule order is important. You have them in the wrong order which forces a double redirect for some requests and/or exposes the rewritten path back out into the URL.

Your 1 - 2 - 3 - 4 - 5 needs to be re-ordered as 4 - 5 - 2 - 1 - 3 and each rule needs [L] flag added.

Check the terminology in your comments. The lines with R=301,L are redirects not rewrites. Adjust the preceding comment accordingly.

flapjack

1:21 am on Nov 20, 2009 (gmt 0)

10+ Year Member



thanks g1smd

I'm a bit confused with which rules you've numbered, have you marked them like this ..

1) RewriteRule ^\-.html index.php
2) RewriteRule ^\dummy.html index.php

#redirect non www to www url

3) RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ [mysite.com...] [R=301]

# rewrite user friendly url from dynamic url
4) RewriteRule ^([^-]*)-category$ /index.php?cat=$1 [L]

#rewrite requests to index.php to [mysite.com...]
5) RewriteCond %{THE_REQUEST} ^[A-Za-z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ [mysite.com...] [R=301]

# redirect old dynamic urls to new friendly urls
6) RewriteCond %{THE_REQUEST} ^[A-Za-z]{3,9}\ /index\.php\?cat=([^\ ]*)\ HTTP/
RewriteRule ^index\.php$ [mysite.com...] [R=301]

jdMorgan

2:46 am on Nov 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure what the "-.html" and "dummy.html" rules were for, and took a guess at the coding, but the correct rule order based on redirects first, rewrites last, each of those groups in order from most-specific to least-specific would be:

# redirect client requests for old dynamic urls to new friendly urls
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php\?cat=([^\ ]*)\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/%1-category? [R=301,L]
#
# redirect client requests for index.php to http://www.example.com/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]
#
# redirect non www hostname requests to www hostname
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#
RewriteRule ^-\.html$ index.php [L]
RewriteRule ^dummy\.html$ index.php [L]
#
# rewrite friendly url to dynamic filepath
RewriteRule ^([^-]+)-category$ index.php?cat=$1 [L]

All tweaks/changes are intentional.

Jim

g1smd

9:27 am on Nov 20, 2009 (gmt 0)

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



I numbered them 1 to 5, not 1 to 6, but Jim has fixed the whole lot up for you anyway.

It was 1 a.m. here and bed was calling.

flapjack

10:22 am on Nov 20, 2009 (gmt 0)

10+ Year Member



Thanks Jim

It worked a treat. Your an absolute star.