BleedObsidian
BleedObsidian

Reputation: 45

Redirect ALL traffic to https including subdomains and remove www

I am trying to create a .htaccess file with the following rules:

http://www.example.com/test => https://example.com/test
http://test.example.com/test => https://test.example.com/test
https://www.example.com/test => https://example.com/test

This is my current .htaccess file:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www.nebla.co.uk$ [NC]
RewriteRule ^(.*)$ http://nebla.co.uk/$1 [R=301,L]

RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

Howerver this causes some odd behaviour:

http://www.nebla.co.uk/ => https://nebla.co.uk/public_html

It also causes a redirect loop on subdomains.

Upvotes: 2

Views: 1148

Answers (1)

BleedObsidian
BleedObsidian

Reputation: 45

I managed to fix my behaviour by instead of using

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.nebla.co.uk$ [NC]
RewriteRule ^(.*)$ http://nebla.co.uk/$1 [R=301,L]
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

I changed $1 to %{REQUEST_URI} like:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.nebla.co.uk$ [NC]
RewriteRule ^(.*)$ http://nebla.co.uk/%{REQUEST_URI} [R=301,L]
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

Upvotes: 1

Related Questions