【问题标题】:Http to Https Redirect Issue. Need some clarificationHttp 到 Https 重定向问题。需要澄清一下
【发布时间】:2023-03-17 10:44:02
【问题描述】:

我试图弄清楚为什么重定向 http 到 https 在所有情况下都不起作用。我在服务器上有两个域。此域具有虚拟主机配置。当我在浏览器服务器上键入www.example.com 时,我会将我重定向到https://www.example.com,并且还可以在没有 www 的情况下使用域的开头。但是,当我输入 http://example.com 时,重定向不起作用。我想我尝试了与 .htaccess 文件相关的所有内容,但没有任何帮助。当我评论所有 RewriteRule 行时,https 重定向也如上所述工作。我完全糊涂了。有人可以帮忙吗?正如您在文件中看到的那样,我注释掉了不起作用的行。

删除此文件

这是我的 .htaccess 文件

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\.
#RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
#RewriteCond %{HTTP_HOST} .
#RewriteCond %{HTTP_HOST} !^www\.kayaon\.com
#RewriteRule (.*) http://www.kayaon.com/$1 [R=301,L]
#RewriteCond %{HTTP:X-Forwarded-Proto} =http 
#RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^(.*)$ index.html?url=$1 [QSA,L]
</IfModule>

像这样在 usr/local/apache/conf.d/vhosts 中更新它

<IfModule mod_rewrite.c>
RewriteEngine On

# add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{REQUEST_SCHEME} =http
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

# route everything to index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.html?url=$1 [QSA,L]
</IfModule>

ssl.conf 文件位于 usr/local/apache/conf.d/vhosts

<VirtualHost 185.86.155.150:8443>
    ServerName kayaon.com
    ServerAlias www.kayaon.com
    ServerAdmin webmaster@kayaon.com
    DocumentRoot /home/kvkkolay/kayaon
    UseCanonicalName Off
    ScriptAlias /cgi-bin/ /home/kvkkolay/kayaon/cgi-bin/

    #CustomLog /usr/local/apache/domlogs/kayaon.com.bytes bytes
    #CustomLog /usr/local/apache/domlogs/kayaon.com.log combined
    ErrorLog /usr/local/apache/domlogs/kayaon.com.error.log

    # Custom settings are loaded below this line (if any exist)
    # IncludeOptional "/usr/local/apache/conf/userdata/kvkkolay/kayaon.com/*.conf"

    SSLEngine on
    SSLCertificateFile /etc/pki/tls/certs/kayaon.com.cert
    SSLCertificateKeyFile /etc/pki/tls/private/kayaon.com.key
    SSLCertificateChainFile /etc/pki/tls/certs/kayaon.com.bundle
    SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown

  

    <IfModule mod_userdir.c>
        UserDir disabled
        UserDir enabled kvkkolay
    </IfModule>

    <IfModule mod_suexec.c>
        SuexecUserGroup kvkkolay kvkkolay
    </IfModule>

    <IfModule mod_suphp.c>
        suPHP_UserGroup kvkkolay kvkkolay
        suPHP_ConfigPath /home/kvkkolay
    </IfModule>

    <IfModule mod_ruid2.c>
        RMode config
        RUidGid kvkkolay kvkkolay
    </IfModule>

    <IfModule itk.c>
        AssignUserID kvkkolay kvkkolay
    </IfModule>

    <Directory "/home/kvkkolay/kayaon">
        Options -Indexes -FollowSymLinks +SymLinksIfOwnerMatch
        AllowOverride All Options=ExecCGI,Includes,IncludesNOEXEC,Indexes,MultiViews,SymLinksIfOwnerMatch
        SSLRequireSSL
    </Directory>
  

</VirtualHost>

<VirtualHost 185.86.155.150:8443>
    ServerName webmail.kayaon.com

    SSLEngine on
    SSLCertificateFile /etc/pki/tls/certs/kayaon.com.cert
    SSLCertificateKeyFile /etc/pki/tls/private/kayaon.com.key
    SSLCertificateChainFile /etc/pki/tls/certs/kayaon.com.bundle
    SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown

    <IfModule mod_proxy.c>
        ProxyRequests Off
        ProxyPreserveHost On
        ProxyVia Full
        ProxyPass / http://127.0.0.1:2095/
        ProxyPassReverse / http://127.0.0.1:2095/

        <Proxy *>
            AllowOverride All
        </Proxy>
    </IfModule>

    <IfModule mod_security2.c>
        SecRuleEngine Off
    </IfModule>

</VirtualHost>

<VirtualHost 185.86.155.150:8443>
    ServerName mail.kayaon.com

    SSLEngine on
    SSLCertificateFile /etc/pki/tls/certs/kayaon.com.cert
    SSLCertificateKeyFile /etc/pki/tls/private/kayaon.com.key
    SSLCertificateChainFile /etc/pki/tls/certs/kayaon.com.bundle
    SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown

    <IfModule mod_proxy.c>
        ProxyRequests Off
        ProxyPreserveHost On
        ProxyVia Full
        ProxyPass / http://127.0.0.1:2095/
        ProxyPassReverse / http://127.0.0.1:2095/

        <Proxy *>
            AllowOverride All
        </Proxy>
    </IfModule>

    <IfModule mod_security2.c>
        SecRuleEngine Off
    </IfModule>

</VirtualHost>

<VirtualHost 185.86.155.150:8443>
    ServerName cpanel.kayaon.com

    SSLEngine on
    SSLCertificateFile /etc/pki/tls/certs/kayaon.com.cert
    SSLCertificateKeyFile /etc/pki/tls/private/kayaon.com.key
    SSLCertificateChainFile /etc/pki/tls/certs/kayaon.com.bundle
    SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown

    <IfModule mod_proxy.c>
        SSLProxyEngine on
        SSLProxyVerify none
        SSLProxyCheckPeerCN off
        SSLProxyCheckPeerName off
        SSLProxyCheckPeerExpire off
        ProxyRequests Off
        ProxyPreserveHost On
        ProxyVia Full

        RewriteEngine on

        RewriteRule ^/roundcube$ /roundcube/ [R]
        ProxyPass /roundcube/ https://127.0.0.1:2031/roundcube/
        ProxyPassReverse /roundcube https://127.0.0.1:2031/roundcube/

        RewriteRule ^/pma$ /pma/ [R]
        ProxyPass /pma/ https://127.0.0.1:2031/pma/
        ProxyPassReverse /pma https://127.0.0.1:2031/pma/

        ProxyPass / https://127.0.0.1:2083/
        ProxyPassReverse / https://127.0.0.1:2083/

        <Proxy *>
            AllowOverride All
        </Proxy>
    </IfModule>

    <IfModule mod_security2.c>
        SecRuleEngine Off
    </IfModule>
    
</VirtualHost>

domianname.conf 文件

<VirtualHost 185.86.155.150:8181>
    ServerName kayaon.com
    ServerAlias www.kayaon.com
    ServerAdmin webmaster@kayaon.com
    DocumentRoot /home/kvkkolay/kayaon
    UseCanonicalName Off
    ScriptAlias /cgi-bin/ /home/kvkkolay/kayaon/cgi-bin/

    #CustomLog /usr/local/apache/domlogs/kayaon.com.bytes bytes
    #CustomLog /usr/local/apache/domlogs/kayaon.com.log combined
    ErrorLog /usr/local/apache/domlogs/kayaon.com.error.log

    # Custom settings are loaded below this line (if any exist)
    # IncludeOptional "/usr/local/apache/conf/userdata/kvkkolay/kayaon.com/*.conf"


    <IfModule mod_setenvif.c>
        SetEnvIf X-Forwarded-Proto "^https$" HTTPS=on
    </IfModule>

    <IfModule mod_userdir.c>
        UserDir disabled
        UserDir enabled kvkkolay
    </IfModule>

    <IfModule mod_suexec.c>
        SuexecUserGroup kvkkolay kvkkolay
    </IfModule>

    <IfModule mod_suphp.c>
        suPHP_UserGroup kvkkolay kvkkolay
        suPHP_ConfigPath /home/kvkkolay
    </IfModule>

    <IfModule mod_ruid2.c>
        RMode config
        RUidGid kvkkolay kvkkolay
    </IfModule>

    <IfModule itk.c>
        AssignUserID kvkkolay kvkkolay
    </IfModule>

    <Directory "/home/kvkkolay/kayaon">
        Options -Indexes -FollowSymLinks +SymLinksIfOwnerMatch
        AllowOverride All Options=ExecCGI,Includes,IncludesNOEXEC,Indexes,MultiViews,SymLinksIfOwnerMatch
    </Directory>

</VirtualHost>

<VirtualHost 185.86.155.150:8181>
    ServerName webmail.kayaon.com

    <IfModule mod_proxy.c>
        ProxyRequests Off
        ProxyPreserveHost On
        ProxyVia Full
        ProxyPass / http://127.0.0.1:2095/
        ProxyPassReverse / http://127.0.0.1:2095/

        <Proxy *>
            AllowOverride All
        </Proxy>
    </IfModule>

    <IfModule mod_security2.c>
        SecRuleEngine Off
    </IfModule>

</VirtualHost>

<VirtualHost 185.86.155.150:8181>
    ServerName mail.kayaon.com

    <IfModule mod_proxy.c>
        ProxyRequests Off
        ProxyPreserveHost On
        ProxyVia Full
        ProxyPass / http://127.0.0.1:2095/
        ProxyPassReverse / http://127.0.0.1:2095/

        <Proxy *>
            AllowOverride All
        </Proxy>
    </IfModule>

    <IfModule mod_security2.c>
        SecRuleEngine Off
    </IfModule>

</VirtualHost>

<VirtualHost 185.86.155.150:8181>
    ServerName cpanel.kayaon.com

    <IfModule mod_proxy.c>
        <IfModule !ssl_module>
            LoadModule ssl_module modules/mod_ssl.so
        </IfModule>
        SSLProxyEngine on
        SSLProxyVerify none
        SSLProxyCheckPeerCN off
        SSLProxyCheckPeerName off
        SSLProxyCheckPeerExpire off
        ProxyRequests Off
        ProxyPreserveHost On
        ProxyVia Full

        RewriteEngine on

        RewriteRule ^/roundcube$ /roundcube/ [R]
        ProxyPass /roundcube/ https://127.0.0.1:2031/roundcube/
        ProxyPassReverse /roundcube https://127.0.0.1:2031/roundcube/

        RewriteRule ^/pma$ /pma/ [R]
        ProxyPass /pma/ https://127.0.0.1:2031/pma/
        ProxyPassReverse /pma https://127.0.0.1:2031/pma/
        
        ProxyPass / https://127.0.0.1:2083/
        ProxyPassReverse / https://127.0.0.1:2083/

        <Proxy *>
            AllowOverride All
        </Proxy>
    </IfModule>

    <IfModule mod_security2.c>
        SecRuleEngine Off
    </IfModule>
    
</VirtualHost>

编辑 1

当我按上述方式更新 .htaccess 文件时,我无法访问我的网站。

您无权访问此服务器上的 /。

【问题讨论】:

    标签: apache .htaccess http redirect https


    【解决方案1】:

    您的代码似乎混合了许多失去其意图的规则。

    在 Apache 2.4+ 上采用这种方式:

    RewriteEngine On
    
    # add www and turn on https in same rule
    RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
    RewriteCond %{REQUEST_SCHEME} =http
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
    
    # route everything to index.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.html?url=$1 [QSA,L]
    

    【讨论】:

    • 感谢您的回答。我尝试了您的代码,但是当我访问网站时,它说网站无法正常工作,www.example.com 会多次重定向您。我将您的代码添加到 mod_rewrite.c 标签中
    • Apache 版本:Apache/2.4.39 @anubhava
    • 您是否有任何机会使用代理?能不能把RewriteCond %{HTTPS} !on换成RewriteCond %{REQUEST_SCHEME} =http,清除浏览器缓存后重新测试。
    • 我替换它并清除缓存但结果相同。
    • 好吧,我又做了同样的结果
    猜你喜欢
    • 2017-01-18
    • 2012-08-05
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    • 2021-07-11
    • 2017-01-01
    相关资源
    最近更新 更多