如何通过.htaccess文件添加HTTPS

时间:2018-01-16 12:03:05

标签: html .htaccess

我在我的网站上安装了SSL,并且我有一个.htaccess文件,其中已经写了一些条件。我已经看过在线显示如何强制https的一些方法。但是,它们似乎都不起作用。我一直在断开链接。

我目前使用的代码,注释掉的规则不适用于https

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /

#This condition doesn't seem to work
#RewriteEngine On
#RewriteCond %{HTTPS} off
#RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Redirect non www. to www.
#RewriteCond %{HTTP_HOST} !^www\.MYWEBSITE\.co\.uk
#RewriteRule (.*) http://www.MYWEBSITE.co.uk/$1 [R=301,L]

# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1? [R=301,L]

# Remove .php ONLY if requested directly
RewriteCond %{THE_REQUEST} (\.php)
RewriteRule ^(.+)\.php /$1 [R=301,L,QSA]

RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php

1 个答案:

答案 0 :(得分:0)

如果您想强制使用HTTPS,可以使用以下命令:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]