Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force HTTPS & Non-WWW for a subdomain dynamically

I'm trying to work out a .htaccess rule that I can use on my main domain & subdomains without requiring changes to the code for each one.

I've tried the following for a subdomain to try and force https and non-www but it doesn't work correctly.

<IfModule mod_rewrite.c>
    # Force HTTPS & NON-WWW
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.test\.website\.com$ [NC] # Detect if it has www?
    RewriteCond %{HTTPS} !=on  [OR]
    RewriteCond %{HTTP_HOST} !^test\.website\.com$ [NC]
    RewriteRule ^ https://test\.website\.com%{REQUEST_URI} [R=301,L]
</IfModule>

Currently if I was to go to http://www.test.website.com/test/ it redirects to https://test%2Cwebsite.com/ so it sort of works but not quite.

I've been trying to do it more dynamically too using:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTPS} !=on  [NC]
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
</IfModule>

But this isn't working at all. What's the best solution to achieve this?

It should essentially be forcing all of the following:

http://website.com/                   => https://website.com/
http://www.website.com/               => https://website.com/
https://www.website.com/              => https://website.com/

http://website.com/testing/           => https://website.com/testing/
http://www.website.com/testing/       => https://website.com/testing/
https://www.website.com/testing/      => https://website.com/testing/

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

http://test.website.com/testing/      => https://test.website.com/testing/
http://www.test.website.com/testing/  => https://test.website.com/testing/
https://www.test.website.com/testing/ => https://test.website.com/testing/
like image 466
Ryflex Avatar asked Jan 04 '23 19:01

Ryflex


2 Answers

You can use this single dynamic rule to achieve all http -> https and www -> non-www redirections:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]

Make sure this rule is right at top and you clear your browser cache before testing this change.

like image 158
anubhava Avatar answered Jan 07 '23 23:01

anubhava


Try this:

Options +FollowSymLinks

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /

    # First, force the HTTPS, whatever it is:
    RewriteCond %{HTTPS} off
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    # Then drop the www, if any:
    RewriteCond %{HTTP_HOST} ^www\.(.*)
    RewriteRule .* https://%1%{REQUEST_URI} [L,R=301]
</IfModule>
## Results:
# http://website.com/      => https://website.com/
# http://www.website.com/  => https://website.com/
# https://www.website.com/ => https://website.com/

# http://website.com/testing/      => https://website.com/testing/
# http://www.website.com/testing/  => https://website.com/testing/
# https://www.website.com/testing/ => https://website.com/testing/

# http://test.website.com/      => https://test.website.com/
# http://www.test.website.com/  => https://test.website.com/
# https://www.test.website.com/ => https://test.website.com/

# http://test.website.com/testing/      => https://test.website.com/testing/
# http://www.test.website.com/testing/  => https://test.website.com/testing/
# https://www.test.website.com/testing/ => https://test.website.com/testing/
like image 23
sepehr Avatar answered Jan 07 '23 22:01

sepehr