CloudSurferUK
CloudSurferUK

Reputation: 95

A better way to URL Rewrite this .htaccess

Could there be a better way of doing this I wonder. It is doing the following:

A) routing mydomain.uk to www.mydomain.uk

B) routing anything non http to https://www.mydomain.uk

I dont use linux based servers much so im sure there is a better way of doing this. End result should be any base url should end up at https://www.mydomain.uk

Heres the code

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

RewriteCond %{HTTP_HOST} ^mydomain.uk$
RewriteRule ^/?$ "https\:\/\/www\.mydomain\.uk\/" [R=301,L]

Upvotes: 1

Views: 20

Answers (2)

Simone Carletti
Simone Carletti

Reputation: 176552

You can use OR

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

The rule RewriteCond %{HTTP_HOST} ^mydomain.uk$ can also be more restrictive and catch any host which is not www.mydomain.uk.

Upvotes: 1

anubhava
anubhava

Reputation: 785866

These 2 rules can be easily combined into one as this one:

RewriteEngine On

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.mydomain.uk%{REQUEST_URI} [L,R=301,NE]

Upvotes: 2

Related Questions