charlie
charlie

Reputation: 481

Redirect all domains to use HTTPS

I have this code in my .htaccess file which does as the comments say:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
Options -Indexes

# redirect all traffic to correct domain
RewriteCond %{HTTP_HOST} ^itl\.|^(www\.)?(integratelecom|integra|integratelecommunications)\b [NC] 
RewriteRule ^ https://www.example.net%{REQUEST_URI} [L,R=301,NE] 

# redirect admin./ssladmin. sub domain to the correct folder
RewriteCond %{HTTP_HOST} (admin|ssladmin)\.itl\.uk\.net$ [NC] 
RewriteRule !^admin/system/ admin/system%{REQUEST_URI} [L,NC]

# redirect subdomain.X to subdomain.example.net
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.(?!itl\.)[^.]+\. [NC]
RewriteRule ^ https://%1.example.net%{REQUEST_URI} [L,NE,R=302]

# map subdomains to correct folder
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.example\.net$ [NC] 
RewriteRule !^subdomains/ subdomains/%1%{REQUEST_URI} [L,NC] 

RewriteRule ^(subdomains/|admin/|index\.php$) - [L,NC] 

# stop external linking to website but allow listed domains below
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.co.uk [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.com [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(.*\.)?itl.uk.net [NC]
#RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.|admin\.|ssladmin\.)?example.net [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ – [NC,F,L]


#######################################
############## MAIN SITE ##############
#######################################

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(blog)/(post|tags|category)/([\w-]+)/?$ index.php?id=$1&type=$2&unique=$3 [QSA,NC,L]
RewriteRule ^(blog)/(archives)/([0-9]{4})/([0-9]{2})?$ index.php?id=$1&type=$2&year=$3&month=$4 [QSA,NC,L]

RewriteRule ^(support/knowledgebase)/(article|category|search)/([\w-]+)?$ index.php?id=$1&type=$2&unique=$3 [QSA,NC,L]

RewriteRule ^([a-zA-Z0-9-/_]+)/?$ index.php?id=$1 [L,QSA]

All of the above is working fine, but i need to force every domain and subdomain to use HTTPS, I have tried adding in:

RewriteCond %{HTTPS} off %HTTP_HOST [NC]
RewriteRule ^ https://%1.domain.net%{REQUEST_URI} [L,NE,R=302]

But that keeps returning an Internal Server Error

I also tried these two lines, but that adds %25 to the end of the URL

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

Upvotes: 0

Views: 96

Answers (1)

Joe
Joe

Reputation: 4897

To force HTTPs you can use:

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

It basically says, if HTTPs is not equal to ON, then it will force the website to display using SSL.

Make sure you clear your cache before testing this.

Upvotes: 1

Related Questions