Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force SSL HTTPS for subdomain with .htaccess

I have this code for forcing SSL on a website generally:

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Which I got from here

I would like it to only apply to a specific sub-domain.

For example, if you were to visit http://sub.domain.org it should redirect to https://sub.domain.org.

I have a hunch that I need to add another RewriteCond which basically says is the domain that is being hit is http://sub.domain.org, but I am at a loss on how to write this code.

I've been looking at other stackoverflow questions such as:

Force HTTPS and WWW for domain and only HTTPS for subdomains HTACESS

Force SSL/https using .htaccess and mod_rewrite

and the Apache docs: https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond

And I've had these attempts:

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^(reports\.)?uksacb\.org$ [NC]
RewriteRule ^.*$ https://%1%{REQUEST_URI} [R,L]

Sort of reversed the answer in this question/answer:

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} =reports.uksacb.org
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

However I'm still failing to make sense of how to do it correctly.

If anyone could explain to me what I'm doing wrong, what I need to do to get it right and how does one go about debugging their rewrite conditions and rules to make sure it works properly I'd be very grateful. Sometimes I think my browser is caching the rules I've written and my different attempts are not taking any affect!

My whole .htaccess looks like this:

Options -MultiViews

RewriteEngine On

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Force SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

like image 893
haakym Avatar asked May 27 '16 10:05

haakym


2 Answers

To avoid using the domain or subdomain names I used to use the following code:

  # Redirect HTTP to HTTPS
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP:X-Forwarded-Proto} !https
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

I prefer without WWW.

%{HTTPS} Set to on if the request is served over SSL.

%{HTTP_HOST} Everything between the protocol (http:// or https://) and the request (/example-file.txt).

%{REQUEST_URI} Everything after the server address — for example, a request to http://example.com/my/example-file.txt sets this value as my/example-file.txt.

%{HTTP:X-Forwarded-Proto} The protocol used when the URL was requested — set to http or https.

I hope it can be useful for anybody.

like image 59
Ruslan P Avatar answered Oct 17 '22 02:10

Ruslan P


For debugging I use a website I found https://htaccess.madewithlove.be/

Using the rules you provided

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

I got an error at

RewriteCond %{HTTPS} !=on

This might work for you

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/%$1 [R,L] 

Found the above example at https://www.sslshopper.com/apache-redirect-http-to-https.html

like image 21
Florian Avatar answered Oct 17 '22 03:10

Florian