Page is a not externally linkable
- Code, Content, and Presentation
-- Apache Web Server
---- 301 redirect best practice


lucy24 - 4:40 am on Sep 7, 2012 (gmt 0)


with this on the old server

RewriteEngine on
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

and this on the new server (thanks to g1smd) who supplied this for me

# 31 - Redirect index requests on new server
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index(\.(php|html?))?
RewriteRule ^(([^/]+/)*)index(\.(php|html?))?$ http://www.example.com/$1? [R=301,L]

# 32 - Redirect .php requests to extensionless URL
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*[^/.]+)\.php
RewriteRule ^(([^/]+/)*[^/.]+)\.php$ http://www.example.com/$1? [R=301,L]

# 33 - Redirect non-canonical requests to www
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Nos. #31 and #32 need to be on BOTH sites so those requests get correctly redirected in a single step, no matter where the user came in. The same applies to ANY request involving any page in any form.
The only rule that won't be exactly parallel is your #33, the one that canonicalizes the host name. That one is only needed on the NEW server. The equivalent on the OLD server is a simple mopping-up that again comes after all the others:

RewriteRule (.*) http://www.example.com/$1 [R=301,L]

I've been using the words "site" and "server" interchangeably. But it really doesn't matter if it's the same server or a different one. The only thing that matters is the changed domain name. Even if both domains are sitting side by side in the same userspace on a shared server, redirects still have to go all the way outside and start over again as if they had never seen the server before.


Thread source:: http://www.webmasterworld.com/apache/4491893.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com