Forum Moderators: phranque

Message Too Old, No Replies

Combine redirect www and index?

         

andrewshim

1:59 pm on Jan 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I currently have this in my htaccess


# Redirect non-www to www URL
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

# Redirect index.php to example.com
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]

Google has got the redirects right, but Yahoo! seems to have dropped ALL my www URLs and only kept non-www URLs. Is my htaccess code okay or is there a way to make it more efficient (somehow combine the 2 instructions above)?

[edited by: jdMorgan at 2:54 pm (utc) on Jan. 30, 2008]
[edit reason] example.com [/edit]

jdMorgan

2:54 pm on Jan 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your rules are reversed, and a request for example.com/index.php will result in two back-to-back redirects.

RewriteEngine on
#
# Redirect client requests for /index.php to www.example.com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]
#
# Redirect requests for all resources in non-www domain to canonical www domain
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

A good general rule is, put your external redirect rules first, in order from most-specific pattern to least-specific, then follow with your internal rewrite rules, again in most-to-least-specific order.

However, I doubt that any of this has anything to do with your Yahoo! problem -- I've seen far worse mistakes with less impact. If your site has been on-line for awhile, be sure to pick the domain variant (www or non-www) that has the most/best incoming links as your canonical domain, unless 'branding issues' are more important to you (Selection of the canonical domain is another subject entirely, and has been much discussed here already).

As noted in many threads here, the now-first rule can optionally be rewritten to handle /index.php requests in any subdirectory if your site has such a problem:


# Redirect client requests for /<any-directory>/index.php to www.example.com/<any-directory>/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://www.example.com/$1 [R=301,L]

Jim

andrewshim

5:58 am on Jan 31, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Jim! I will reverse my code!