I have an SSL for www.example.com, and a bunch of alias domains (e.g. mydomain.co.uk, otherdomain.com etc. etc.) parked on the cPanel account.
What I want to do is force the use of https://www.example.com for all requests. Anything after the domain should be appended as it is (which is also rewritten for SEO friendly URLs later in .htaccess).
For example: –
http://example.com/page/2/
https://example.com/page/2/
http://www.otherdomain.com/page/2/
http://mydomain.co.uk/page/2/
should all become
https://www.example.com/page/2/
This almost works: –
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
But can it be done in one go? Also, the above is appending the query string again to the URL for example: –
http://www.example.com/page/2/
is ending up as: –
https://www.example.com/page.php/2/?2/
The search engine friendly SEO part is: –
Options +SymlinksIfOwnerMatch +MultiViews
RewriteRule ^(.*).php/(.*) $1.php?$2
Which should remain unchanged.
IIRC, before I forced the SSL, the forcing of just www.example.com was working fine as: –
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http%2://www.example.com/$1 [R=301,L]
Any help much appreciated!
2
Answers
To force www and https in a single redirection, you can use the following rule :
Try this version. It uses the original request sent by the browser so should be immune to other rewriting. If you still experience the extra junk, it’s coming from somewhere else.
The extra, commented rule is in case you need to avoid this redirect on your development server.