重定向所有域以使用HTTPS

时间:2017-05-18 07:37:49

标签: .htaccess

我的.htaccess文件中包含此代码,其注释如下:

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]

所有上述工作正常,但我需要强制每个域和子域使用HTTPS,我尝试过添加:

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

但是这会不断返回内部服务器错误

我也试过这两行,但是在网址的末尾添加了%25

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

1 个答案:

答案 0 :(得分:1)

要强制HTTPs,您可以使用:

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

它基本上说,如果HTTP不等于ON,那么它将强制网站使用SSL显示。

确保在测试之前清除缓存。