【问题标题】:URL is broken in Apache HTTPSApache HTTPS 中的 URL 已损坏
【发布时间】:2015-04-28 13:27:22
【问题描述】:

我的网站在 https 中时,我的网站中的所有链接都损坏了。无论如何,我需要做的是将所有请求从http 重定向到https,所以我在.htaccess 中添加了以下代码。

# Redirect from http to https.
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^project-local\.com*
RewriteRule ^(.*)$ https://project-local.com/$1 [L,R=301]

重定向工作正常,但是如果我在 https 中单击站点中的任何链接会发生什么,所有响应都是 404。

这是我的 ssl vhost 的样子:

<IfModule mod_ssl.c>
    <VirtualHost _default_:443>
        ServerName project-local.com
        DocumentRoot /home/snake/repo/project-a/docroot

        ErrorLog ${APACHE_LOG_DIR}/project-a-ssl_error.log
        CustomLog ${APACHE_LOG_DIR}/project-a-ssl_access.log common
        SSLEngine on

        SSLCertificateFile /etc/apache2/ssl/apache.crt
        SSLCertificateKeyFile /etc/apache2/ssl/apache.key

        <FilesMatch "\.(cgi|shtml|phtml|php)$">
            SSLOptions +StdEnvVars
        </FilesMatch>
        <Directory /usr/lib/cgi-bin>
            SSLOptions +StdEnvVars
        </Directory>

        <Directory /home/snake/repo/project-a/docroot/>
            Require all granted
        </Directory>

        BrowserMatch "MSIE [2-6]" \
                                        nokeepalive ssl-unclean-shutdown \
                                        downgrade-1.0 force-response-1.0
        # MSIE 7 and newer should be able to use keepalive
        BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
    </VirtualHost>
</IfModule>

【问题讨论】:

    标签: apache .htaccess http-status-code-404


    【解决方案1】:

    您的http-&gt;https 规则不正确,可能导致无限重定向,您可以使用:

    RewriteCond %{HTTP_HOST} ^project-local\.com$ [NC]
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301,NE]
    

    【讨论】:

    • 我根据您的回答修改了规则,但仍然有 404。
    • 哦,另一件事是网站布局也被破坏了。缺少 CSS 文件。
    • 该 URL 已正确重定向到 https。您确定 SSL 中显示的 DocumentRoot 与非 SSL 站点相同吗?
    • @anubhava ....是的,先生,我确信 ServerNameDocumentRoot 在 http 和 https 虚拟主机上都是相同的。
    • 那么为什么是404?此重定向适用于 http-&gt;https 重定向,并且发生正确。你甚至可以重命名这个 .htaccess 或者注释掉这个建议的规则,直接https://project-local.com/about-us。看看页面是否正确打开。
    猜你喜欢
    • 1970-01-01
    • 2018-06-22
    • 2011-09-04
    • 2015-10-15
    • 1970-01-01
    • 2014-10-06
    • 2012-04-28
    • 1970-01-01
    • 2021-02-10
    相关资源
    最近更新 更多