cancel
Showing results for 
Search instead for 
Did you mean: 

.htaccess 301 redirects not working properly

SOLVED

.htaccess 301 redirects not working properly

I just moved my Magento site to a new domain, the URL structure is unchanged so in order to preserve SEO I want to set up a global 301 redirect from oldsite.com to newsite.com.

 

It needs to work in such a way that for example oldsite.com/product1 redirects to newsite.com/product1 and so on.

 

I found many suggestions, but no matter which code I use, oldsite.com/product1 always redirects to the main domain newsite.com, without the rest of the URL.

 

I tried this:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.com$ [NC]
RewriteRule ^ http://newsite.com%{REQUEST_URI} [R=301,L,NE]

Also this:

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

Both versions only seem to redirect to the main domain, even though they are supposed to preserve the whole URL structure. Where am I going wrong? Thanks

1 ACCEPTED SOLUTION

Accepted Solutions

Re: .htaccess 301 redirects not working properly

Well... this worked:

 

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.newsite.com$ [NC] 
RewriteRule ^(.*)$ http://www.newsite.com/$1 [R=301,L]
</IfModule>

Don't know why, might be to do with the fact that I added www to the redirect URL. Anyway, all sorted.

View solution in original post

2 REPLIES 2

Re: .htaccess 301 redirects not working properly

Well... this worked:

 

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.newsite.com$ [NC] 
RewriteRule ^(.*)$ http://www.newsite.com/$1 [R=301,L]
</IfModule>

Don't know why, might be to do with the fact that I added www to the redirect URL. Anyway, all sorted.

Re: .htaccess 301 redirects not working properly

The reasons for 301 redirect not working are much more well-defined among WordPress sites. One of the main causes is because you have added the rewrite rules on both the cPanel “Htaccess Redirect Generator” tool and from your WordPress plugin
 
Because the WordPress 301 redirect is not always reliable, we recommend issuing the 301 redirect via your . htaccess file. Another benefit is that the . htaccess redirect is slightly faster than redirecting via PHP, because it is loaded even before the rest of the page.