skip to Main Content

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


  1. To force www and https in a single redirection, you can use the following rule :

    RewriteEngine on
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} !^www. [NC]
    RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,NE,R]
    
    Login or Signup to reply.
  2. 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.

    RewriteEngine on
    RewriteCond %{HTTPS} =off [OR]
    RewriteCond %{HTTP_HOST} !=www.example.com [NC]
    #RewriteCond %{HTTP_HOST} !=your.dev.server [NC]
    RewriteCond %{THE_REQUEST} ^S++s++(S++)
    RewriteRule ^ https://www.example.com%1 [DPI,NE,L,R=301]
    

    The extra, commented rule is in case you need to avoid this redirect on your development server.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search