.htaccess 如何强制网站打开www和https与htaccess

goucqfw6  于 2022-11-16  发布在  其他
关注(0)|答案(2)|浏览(199)

我无法将我的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,但如果我手动删除www从url,它不会重定向到www。

ltqd579y

ltqd579y1#

谢谢大家的支持,
我已经解决了我的问题,这是因为ssl是安装在我的网站上,因此在/etc/apache2/sites-available/
我发现default-ssl.conf文件,其中包含一些设置,我猜不允许我ovveride与.htaccess
但现在添加了

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

htaccess工作正常。

jckbn6z7

jckbn6z72#

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

相关问题