Mobile app version of vmapp.org
Login or Join
Cugini213

: 301 redirect for new domain whilst maintaining page paths for WordPress site My client has a WordPress site which is hosted on a CPanel Linux server. Their old URL is https://old-example.co.uk.

@Cugini213

Posted in: #301Redirect #Redirects

My client has a WordPress site which is hosted on a CPanel Linux server.

Their old URL is old-example.co.uk. Their new URL is 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 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 ^(.*)$ new-example.co.uk/ [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 old-example.co.uk directly via the browser it redirects to new-example.co.uk perfectly.

However, when you click on a an old link via SERPs, e.g. 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 ^(.*)$ new-example.co.uk/ [L,R=301]
</IfModule>
#Force SSL
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ new-example.co.uk/ [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?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Cugini213

1 Comments

Sorted by latest first Latest Oldest Best

 

@Ogunnowo487

With the above code, when you visit old-example.co.uk directly via the browser it redirects to new-example.co.uk perfectly.


"The above code" does not actually do that. The only redirect in "the above code" is an HTTP to HTTPS (only) redirect. So, it would redirect old-example.co.uk (not https) to new-example.co.uk. So, if you were seeing this redirect then something else was doing it, perhaps WordPress itself or even a cached redirect?


...fails to load some images and stylesheets.


What do you mean exactly by "some images and stylesheets"? So, some images and stylesheets are loading correctly? What is different about the images and stylesheets that fail to load?

Images and stylesheets should not be getting redirected anyway. These should already be linked correctly in the HTML. (?)

However, your updated code does contain an error (although not an error that would necessarily fix "some" of your images and stylesheets) and it could certainly be simplified...

#Redirect old domain
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !new-example.co.uk$ [NC]
RewriteRule ^(.*)$ new-example.co.uk/ [L,R=301]
</IfModule>
#Force SSL
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ new-example.co.uk/ [R,L]
</IfModule>



The "error" is in the first RewriteCond directive:


RewriteCond %{HTTP_HOST} !new-example.co.uk$ [NC]



You are missing the start-of-string anchor on the CondPattern, so this condition is successful when the host does not end with new-example.co.uk, so it will fail to canonicalise a request for new-example.co.uk (the www subdomain), since this also ends with new-example.co.uk.

The above should be rewritten:

# Redirect old domain
RewriteCond %{HTTP_HOST} !^new-example.co.uk$
RewriteRule (.*) new-example.co.uk/ [R=301,L]

# Force SSL
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) new-example.co.uk/ [R=301,L]


Notes for the above:


As discussed above, the start of string anchor (^) has been added to the CondPattern: !^new-example.co.uk$ - also note the escaped dots, otherwise these match any character.
No need for the <IfModule> wrappers. (See my answer to this recent question: Is Checking For mod_write Really Necessary?)
No need for the additional RewriteEngine and RewriteBase directives. These are already stated in the WordPress code block. The directives specified later in the file (in the WP block) override the earlier directives anyway.
Removed the NC flag on the negated condition. (Note it is a negated condition.) Since you want it to be successful when it is not new-example.co.uk only, all lowercase. If you include the NC flag then it will fail to match malformed requests such as NEW-EXAMPLE.CO.UK.
The "Force SSL" redirect should ultimately be a 301 as well. By stating just R then it will default to a temporary 302 redirect. (Although it is good to test with 302 redirects in order to avoid browser caching.)
I changed the RewriteRule pattern from ^(.*)$ to (.*) - these two regex are essentially the same, since * is greedy by default. (.*) is shorter/simpler so is generally preferred. However, some people do still prefer to include the start/end anchors as they find it easier to read.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme