![](/img/trans.png)
[英]Apache Vhosts HTTP and HTTPS (both) redirect to another domain
[英]Is there a uniform https redirect method that should work for both cPanel and VestaCP when apache htpasswd is enabled?
我經營在 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 它會壞掉。 如果您嘗試以任何不完全限定的 URL 登錄,您最終會出現超時、未找到頁面,例如:
www.website.ext401.shtml.
如上所示,ext 和 401.html 一起運行。 主頁錯誤是:無法訪問該站點。 如果您嘗試以https://www.website.ext身份登錄,則它可以正常工作。
修復 cPanel 問題很重要,VestaCP 會很好。
在承包商朋友的大力幫助下,我們提出了我在多個實時和開發(訪問受限)網站上設置的解決方案:
$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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.