Http 到 Https 重定向问题。需要澄清一下

Http to Https Redirect Issue. Need some clarification

我想弄清楚为什么将 http 重定向到 https 并非在所有情况下都有效。我在服务器上有两个域。此域具有虚拟主机配置。当我键入 www.example.com to browser server redirect me to https://www.example.com and also working with out www on to begin of the domain. However, when I type 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/ [R=301,L]
#RewriteCond %{HTTP:X-Forwarded-Proto} =http 
#RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^(.*)$ index.html?url= [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= [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 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= [QSA,L]