I have a website I am looking to redirect .com traffic to .co.uk.

For the moment, when I navigate to https://example.com, it redirects to https://www.example.co.uk and displays as such in the address bar

However when I navigate to something like https://example.com/contact-us, it does not redirect to the .co.uk equivalent.

My .htaccess is currently configured like so:

RewriteCond %{HTTP_HOST} !^(www\.)?example\.co.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]

What exactly am I missing?

  • It seems that if I navigate to http:// example.com/example it will redirect to https:// www.example.co.uk/example – Stephen Carroll Oct 16 '19 at 14:51
  • But if I navigate to https:// example.com/example, it will still display that address up in the bar, and will direct the client to www. example.co.uk/ when I click anything on screen – Stephen Carroll Oct 16 '19 at 14:53
  • "...and will direct the client to https://www.example.co.uk/ when I click anything on screen" - literally "anything", or when you click a link? And to the document root, /example is lost? – MrWhite Oct 16 '19 at 18:52

Since this is a WordPress site, you presumably have other mod_rewrite directives in your .htaccess file to drive the WP "pretty" permalinks.

The behaviour you are experiencing would seem to be consistent with having put the redirect directives in the wrong place, ie. after the WordPress front-controller*1. This redirect must go before the existing WordPress directives.

*1 With the mod_rewrite redirect after the default WordPress front-controller (the mod_rewrite directives that route the request to WordPress) the redirect will only be processed for the document root and any direct request for a physical file or directory. All other requests will be "blocked" by the WP front-controller (and routed to WP) so the redirect that follows is not processed.

However, your condition is not strictly correct either, assuming you do want to canonicalise/redirect example.co.uk as well? Your current RewriteCond directive is only satisfied if the requested host is not www.example.co.uk and not example.co.uk (since you made the www subdomain in a negated condition. The NC flag should also be omitted here as well in order to redirect incorrectly cased requests for the canonical hostname.

Try the following instead, near the top of your .htaccess file:

RewriteCond %{HTTP_HOST} !^www\.example\.co\.uk$
RewriteRule (.*) https://www.example.co.uk/$1 [R=301,L]

# BEGIN WordPress
# ... WP directives follow...

Redirect anything other than www.example.co.uk (exactly) before the WordPress front-controller.

  • How did you get on with this? – MrWhite Oct 29 '19 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.