我使用 Apache 服务器来发布我的网站。问题是我想强制我的网站使用 HTTPS。例如,如果有人输入 URL“ http://www.alireza.co/contact.html ”,它应该重定向到“ https://alireza.co/contact.html ”。
我.htaccess
用这些规则创建了一个文件:
# To use 404.html as default 404 error page.
ErrorDocument 404 /404.html
# To force website use https and remove www. subdomain.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://alireza.co/$1 [R=301,L]
现在的问题是,每当我输入 URL 时,无论是否带有https
或http
,或者带有www.
或不带有它,我都会收到错误消息too many redirects
或the website is not redirecting properly
在 Firefox 中。
我使用Autistici.org作为我的主机提供商。我的网站是静态的(仅限 HTML/CSS)。我尝试了很多方法,但没有成功。
我只是想通了。我唯一要做的就是.htaccess
用这些简单的规则重写我的文件:
# To remove www. from URL
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.alireza\.co [NC]
RewriteRule ^(.*)$ https://alireza.co/$1 [L,R=301]
#To force HTTPS
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# To make 404.html default 404 error page
ErrorDocument 404 /404.html
.htaccess
删除规则很简单,www.
但我将其重定向到https://alireza.co/ $1 而不是http://alireza.co/ $1。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句