3

I have example.co.uk and example.com domain names. The main website is example.co.uk. I have an SSL certificate on the domain example.co.uk.

I want the .htaccess file to 301 redirect everything seen in a browser to https: without affecting SEO.

This means entries of:

  • example.co.uk
  • example.com
  • www.example.com
  • www.example.co.uk
  • http://example.co.uk
  • http://example.com
  • http://www.example.co.uk
  • http://www.example.com
  • https://example.co.uk
  • https://example.com
  • https://www.example.com

would all forward to https://www.example.co.uk.

This is a single-page site, so "catch-all" type solution will work. Ideally, I'd like this to work for a multipage site. So I could use the .htaccess code again in different projects.

I came up with the following, but it converts .com entries to https://example.com or https://www.example.com then stops:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.co.uk$ [NC]
RewriteRule ^ https://www.example.co.uk%{REQUEST_URI} [L,NE,R]
  • You need a valid SSL cert on both domains to redirect from https://www.example.com and https://example.com to example.co.uk regardless if it's just a redirect, this is because your actually visiting a website to obtain the redirect and not having one will stop the user with a warning. You can use Cloudflare which will provide a free SSL cert and redirect all to the example.co.uk website using page rules. – Simon Hayter Jul 20 '17 at 8:37
1

I have an SSL certificate on the domain example.co.uk.

Presumably this also covers the www subdomain? (It would need to.)

https://example.com
https://www.example.com

As mentioned by Simon in comments, in order to redirect https://example.com you will need an SSL cert that covers the example.com domain. Otherwise, the browser will stop at the invalid certificate warning. (The SSL handshake occurs before your server is able to process the request.)

I came up with the following, but it converts .com entries to https://example.com or https://www.example.com then stops:

Presumably because you are getting an invalid certificate warning?

Since you have multiple domains, it is perhaps easier to construct a rule that redirects anything that is not the canonical host, rather than trying to positively match everything that it could be. For example:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !=www.example.co.uk
RewriteRule ^ https://www.example.co.uk%{REQUEST_URI} [R,L]

This combines both HTTPS and www canonicalisation into a single rule.

The ! prefix on the CondPattern negates the expression, so it is successful when it does not match the given string/regex. In this case, it is successful when the host is not the string www.example.co.uk. The = prefix is the exact match (lexicographical comparison) operator, so the remaining CondPattern is seen as an ordinary string, not a regex.

Do you need the NE (noescape) flag? You omitted it on the first rule, so you'd naturally lose any special characters that passed through this directive anyway.


RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.co.uk$ [NC]
RewriteRule ^ https://www.example.co.uk%{REQUEST_URI} [L,NE,R]

Aside: Since you are using HTTP_HOST in the substitution in the first RewriteRule, if you switched the order of these rules then you'd avoid an unnecessary second redirect when accessing http://example.co.uk etc. Since it is only after the second rule that you know the host is canonicalised.

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.