Rick Alvarez
Rick Alvarez

Reputation: 191

What's wrong with this htaccess?

I'm trying to move a site from a set of static html pages on one domain, to a new domain on WordPress. So I want each old page redirect to the corresponding new page on the new domain.

The structure on old domain is like so:

http://www.example.com/page.html

and the new domain has this structure:

http://www.example-new.com/category/sub-category/page/

So I tried this:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^http://www.domain-old\.com/page1.html [NC]
RewriteRule ^(.*)$ http://www.domain-new.com/new-category/new-sub-category/page1/ [R=301,L]

RewriteCond %{HTTP_HOST} !^http://www.domain-old\.com/page2.html [NC]
RewriteRule ^(.*)$ http://www.domain-new.com/new-category/new-sub-category/page2/ [R=301,L]

RewriteCond %{HTTP_HOST} !^http://www.domain-old\.com/page3.html [NC]
RewriteRule ^(.*)$ http://www.domain-new.com/new-category/new-sub-category/page3/ [R=301,L]

RewriteCond %{HTTP_HOST} !^http://www.domain-old\.com/page4.html [NC]
RewriteRule ^(.*)$ http://www.domain-new.com/new-category/new-sub-category/page4/ [R=301,L]

RewriteCond %{HTTP_HOST} !^http://www.domain-old\.com/page5.html [NC]
RewriteRule ^(.*)$ http://www.domain-new.com/new-category/new-sub-category/page5/ [R=301,L]

And it works (kinda). The only GIANT problem is that now all pages redirect to the first new page, in this case http://www.domain-new.com/new-category/new-sub-category/page1/

Obviously I'm not an expert, but I spent hours on Google looking for an answer and trying things and can't figure how to do this, the closest I have found is about redirecting domain A to domain b removing the extension, so this is my last resource.

Any help appreciated!

Upvotes: 1

Views: 32

Answers (2)

BA_Webimax
BA_Webimax

Reputation: 2679

You are over-complicating this. Instead of all that processing, just use the Redirect Directive.

Redirect 301 /page1.html /new-category/new-sub-category/page1/
Redirect 301 /page2.html /new-category/new-sub-category/page2/
Redirect 301 /page3.html /new-category/new-sub-category/page3/
Redirect 301 /page4.html /new-category/new-sub-category/page4/

Upvotes: 1

anubhava
anubhava

Reputation: 785521

This condition is the main problem:

RewriteCond %{HTTP_HOST} !^http://www.domain-old\.com/page1.html [NC]

This will always evaluate to true because variable HTTP_HOST only matches host name part of a web request not the complete URL.

You can use this single rule in your .htaccess:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?:www\.)?domain-old\.com$ [NC]
RewriteRule ^([\w-]+)\.html$ http://www.domain-new.com/new-category/new-sub-category/$1/ [R=301,L,NC]

Upvotes: 2

Related Questions