URL 在 Apache HTTPS 中损坏

URL is broken in Apache HTTPS

我遇到一个问题,当我的站点处于 https 时,我站点中的所有 link 都被破坏了。无论如何,我需要做的是将所有请求从 http 重定向到 https,所以我在 .htaccess.

中添加了下面的代码
# Redirect from http to https.
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^project-local\.com*
RewriteRule ^(.*)$ https://project-local.com/ [L,R=301]

重定向工作正常,但如果我在站点中单击任何 link 时会发生什么情况,而所有响应都是 404。

这是我的 ssl 虚拟主机,如下所示:

<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>

您的 http->https 规则不正确,它会导致无限重定向,您可以使用:

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