Mobile app version of vmapp.org
Login or Join
Pierce454

: Htaccess 301 to redirect .com to .co.uk, http to https and non-www to www subdomain I have example.co.uk and example.com domain names. The main website is example.co.uk. I have an SSL certificate

@Pierce454

Posted in: #Htaccess #Http #Https

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 example.com www.example.co.uk example.co.uk http://example.com www.example.co.uk http://www.example.com example.co.uk https://example.com www.example.com

would all forward to 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 example.com or www.example.com then stops:

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

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

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Pierce454

1 Comments

Sorted by latest first Latest Oldest Best

 

@Alves908

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


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

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



As mentioned by Simon in comments, in order to redirect 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 example.com or 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 ^ 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 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 ^(.*)$ %{HTTP_HOST}%{REQUEST_URI} [L,R]

RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example.co.uk$ [NC]
RewriteRule ^ 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 example.co.uk etc. Since it is only after the second rule that you know the host is canonicalised.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme