Forum Moderators: phranque

Message Too Old, No Replies

Redirecting old site to new site - different link structures

         

Aunt_Clara

8:31 am on Aug 31, 2012 (gmt 0)

10+ Year Member



I have my moved my site www.oldexample.com to www.example.com. The links structures are completely different. There were over a 100 pages in the new site, so I wrote redirects for each link. So far the homepage redirects fine, the rest of the redirects don't work.

This is the htaccess I am using (clipped for brevity):

# AddHandler application/x-httpd-php5 .php .html
<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ [example...] [R=permanent,L]
Redirect 301 /2012/04/swiss-army-room.html$ http://www.example.com/972-swiss-army-room.html
Redirect 301 /2012/04/swiss-army-room.html$ http://www.example.com/972-swiss-army-room.html
Redirect 301 /2011/04/monsters-and-princesses.html$ http://www.example.com/9366-monsters-and-princesses.html
Redirect 301 /2011/03/giant-spring-greeting-card.html$ http://www.example.com/963-a-giant-spring-greeting-card.html
</IfModule>

Going to the old oldexample.com redirects to www.example.com, as I wanted, the individual links give this error:

Not Found

The requested URL /2011/04/monsters-and-princesses.html was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.


Can you please let me know what I am doing wrong? I have been trying to figure it out for half a day without success. Thanks in advance.

Aunt_Clara

9:07 am on Aug 31, 2012 (gmt 0)

10+ Year Member



Nevermind. I found the problem. The old and new domain were served from the same server. What worked was this:

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

Then the individual rewrites.

g1smd

6:43 pm on Aug 31, 2012 (gmt 0)

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



Now you have a double redirect for many of the requests. That IS a disaster.

Do not use Redirect at all. Use a RewriteRule for each rule.

The domain canonicalisation rule must be last.

Escape all literal periods in patterns.

Do not use .* at the beginning or in the middle of any pattern.

lucy24

8:18 pm on Aug 31, 2012 (gmt 0)

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



I found the problem. The old and new domain were served from the same server.

That should make no difference. Heck, they could be in the same userspace. A redirect doesn't care. Just be sure to put the htaccess in a location where it will only be seen by requests for the old domain name.

The overarching problem is

<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ [example...] [R=permanent,L]
Redirect 301 /2012/04/swiss-army-room.html$ http://www.example.com/972-swiss-army-room.html
Redirect 301 /2012/04/swiss-army-room.html$ http://www.example.com/972-swiss-army-room.html
... et cetera


You only need to say RewriteEngine On once per htaccess. Won't cause a disaster, but isn't needed and can be a symptom of careless cut-and-paste work-- like the duplicate lines immediately afterward.

More seriously, you've got Redirects using mod_alias inside the "IfModule" rewrite envelope. Now, "Do you have mod_rewrite?" is pretty much a rhetorical question. You don't need the envelope at all. But it means among other things

-- you're mixing up two modules that won't necessarily execute in the order you've written the code, because each module is an island.

-- only the first redirect has a Condition.

Oh, and the opening anchor in ^example.com excludes requests for www.example.com. The revised version with .* is worse. Since you are redirecting rather than rewriting, you don't need anchors at either end. Unless :: shudder :: the name of one domain is wholly contained within the name of the other, as in "example.com" and "oldexample.com" if they were real names.