Search code examples
.htaccesshttp-status-code-301http-redirect

why my 301 redirect is not work?


I have a blog using wordpress. I changed the domain name, so I want to do 301 redirect for all request.

My .htaccess file:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} ^old.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.old.com$ 
RewriteRule ^(.*)$ http://www.new.org/$1 [R=301,L]
</IfModule>


# END WordPress

The indented three line is new added for the domain name change.

It can only redirect the old.com and www.old.com to www.new.org, but can't redirect old.com/XXX to www.new.org/XXX.

I think the original rule is conflicted with the new added one. But I don't know how to change it.


Solution

  • You should never modify anything between # BEGIN WordPress and # END WordPress. It's just one of those WordPress no-no's. WordPress will overwrite anything between those two entries, so put code above or below it to ensure nothing gets wiped out.

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^old.com$ [NC,OR]
    RewriteCond %{HTTP_HOST} ^www.old.com$ 
    RewriteRule ^(.*)$ http://www.new.org/$1 [R=301,L]
    </IfModule>
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress