无法将http://example.com重定向到https://www.example.com,这是最好的方法

时间:2017-11-25 16:10:46

标签: .htaccess redirect apache2

我无法将我的example.in指向https://www.example.in 我正在使用.htaccess

RewriteEngine On
RewriteCond %{SERVER_PORT} 80

RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if 
not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
AcceptPathInfo On

我在stackoverflow上找到了这个代码,但它在我的项目中没有用 我的网站在AWS上并使用apache2 php 7.0的虚拟主机

它不是强制www和我尝试过其他代码.htaccess但是如果我从url手动删除www它不会被重定向到www。

1 个答案:

答案 0 :(得分:0)

谢谢大家的支持,

我已解决了我的问题,因为ssl已在我的网站上安装,因此在/etc/apache2/sites-available/

我找到default-ssl.conf文件,其中包含一些设置,我猜不允许我使用.htaccess进行切换。

但现在添加

之后
<Directory /var/www/myfolder > AllowOverride All </Directory>

htaccess工作正常。