Search code examples
wordpress.htaccesshttp-redirect

redirect site excluding wp-admin when redirecting each page with individual 301's


I am using htaccess to redirect traffic from one site to another using multiple redirects as per below. I am mapping each page so the content is still relevant for SEO purposes.

redirect 301 /page https://newsite.co.uk/page

I have 407 redirects mapped this way.

I need to also redirect the wordpress home page but if I use redirect 301 / https://newsite.co.uk it obviously redirects all traffic. Can anyone point me in the right direction how to redirect the wordpress home page but leaving the wp-admin folder, and all of my custom redirects?

This is the current .htaccess structure:

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

# START MJB REDIRECTS
redirect 301 /resetpass/ https://www.newsite.co.uk/newpage
redirect 301 /login/ https://www.newsite.co.uk/anothernewpage
407 more redirects...

Solution

  • First thing first this rule:

    redirect 301 / https://newsite.co.uk
    

    Must be placed at the bottom so that it should be the last rule to execute otherwise this will override all other rules and will always execute.

    Using redirect should be avoided altogether since you already have mod_rewrite rules in your .htaccess. So above rule can be rewritten as:

    RewriteRule ^/?$ https://newsite.co.uk [L,R=302]
    

    Finally if you have access to server config I highly recommend using RewriteMap since you have large numbers of URIs to redirect.

    PS: Only after you verify redirect is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.