问题描述
我经营在 VestaCP 和 cPanel 下运行的网站。在开发网站时,我设置了 htpassword 保护,密码语法如下:
AuthType Basic
AuthUserFile /home/the-account/htpasswd
AuthName "Restricted"
Require valid-user
用于重定向以将所有内容重定向到 https://www.the-website...我使用:
RewriteEngine on
RewriteBase /
Options +FollowSymlinks
# Redirect http to https
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.website.ext$1 [R,L]
如果未启用密码保护,重定向行可以正常工作。全部:
website.ext
www.website.ext
http://website.ext
重定向到:
https://www.website.ext (the goal)
然而,VestaCP 与 cPanel 的行为不同,并且在 cPanel 中严重中断。在vesta中,当您访问像 website.ext 这样的 url 时,它会显示 apache 登录屏幕,首先您必须以 http:// 登录,然后第二次以 https://www 登录。但它有效,你最终达到了目标。
使用 cPanel 它会坏掉。如果您尝试以任何不完全限定的网址登录,您最终会出现超时、未找到页面,例如:
www.website.ext401.shtml.
如上所示,ext 和 401.html 一起运行。主页错误是: 无法访问该站点。如果您尝试以 https://www.website.ext 的身份登录,它可以正常工作。
修复 cPanel 问题很重要,VestaCP 会很好。
解决方法
在承包商朋友的大力帮助下,我们提出了一个解决方案,我在多个实时和开发(访问受限)网站上设置了该解决方案:
文件第一行的Error Document 行解决了启用Apache密码限制时cPanel中的“ErrorDocument 401”问题
$website.$ext ==> your-website.com
$account ==> account folder of the home directory where the htpasswd file is stored; /home/home-folder
-------------- 剪断 ---------------
ErrorDocument 401 default
# Redirect http to https
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.$website.$ext/$1 [L,R=301]
# BEGIN-APACHE-RESTRICTION
<If " %{HTTPS} != 'off' && %{HTTP_HOST} == 'www.$website.$ext'">
AuthType Basic
AuthUserFile /home/$account/htpasswd
AuthName "restricted area"
Require valid-user
</If>
# END-APACHE-RESTRICTION