My client has a WordPress site which is hosted on a CPanel Linux server.
Their old URL is https://old-example.co.uk
. Their new URL is https://new-example.co.uk
.
Both URLs are pointing to the same A record and have been added as domain aliases via CPanel.
All database URLs have been replaced with https://new-example.co.uk
using Interconnect's Search & Replace tool. wp-config.php also has been updated to reflect the new URL.
We then tried this via .htaccess:
#Force SSL
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://new-example.co.uk/$1 [R,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
With the above code, when you visit https://old-example.co.uk
directly via the browser it redirects to https://new-example.co.uk
perfectly.
However, when you click on a an old link via SERPs, e.g. https://old-example.co.uk/about
, the old URL remains in the browsers address bar.
We'd like the new URL to always appear in the browsers address bar. To achieve this, we tried adjusting our .htaccess like so:
#Redirect old domain
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !new-example.co.uk$ [NC]
RewriteRule ^(.*)$ https://new-example.co.uk/$1 [L,R=301]
</IfModule>
#Force SSL
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://new-example.co.uk/$1 [R,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
This hasn't work and fails to load some images and stylesheets.
Where are we going wrong here?
https://old-example.co.uk/about
"directly"? As @Stephen suggests, there should be no difference between typing the URL directly in the browser and clicking on a URL in the SERPs. "With the above code, when you visithttps://old-example.co.uk
directly, it re-directs tohttps://new-example.co.uk
perfectly." - the above code is not doing that (probably WP itself).www
needs to match between theRewriteCond
and theRewriteRule
They should either both have thewww
or both not. That rewrite rule and the force SSL rule need to come before the WordPress rewrite rules.