HTTP 到 HTTPS Apache 重写不起作用



我有一个在 Ubuntu 上运行的 apache2 服务器,我正在玩弄它来提高我的系统管理技能。我一直在尝试在整个站点中使用HTTPS设置Web服务器。目前,如果我去 https://mysite.com 它工作得很好。我已经尝试了几种不同的重定向规则,以使与该站点的所有连接都强制使用HTTPS,但我没有运气。谁能在这里指出我的错误?

httpd.conf:

NameVirtualHost *:443
<VirtualHost *:443>
        ServerName mysite.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/
        SSLEngine on
        SSLOptions +FakeBasicAuth -StrictRequire +ExportCertData
        SSLCertificateKeyFile /etc/ssl/crt/myserver.key
        SSLCertificateFile /etc/ssl/crt/mysite_com.crt
        SSLCertificateChainFile /etc/ssl/crt/mysite.ca-bundle
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride AuthConfig Options FileInfo Limit
                Order allow,deny
                allow from all
        </Directory>
</VirtualHost>
<IfModule !mod_rewrite.c>
    LoadModule rewrite_module modules/mod_rewrite.so
</IfModule>

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [R,L]

端口会议:

NameVirtualHost *:80
Listen 80
<IfModule mod_ssl.c>
    # If you add NameVirtualHost *:443 here, you will also have to change
    # the VirtualHost statement in /etc/apache2/sites-available/default-ssl
    # to <VirtualHost *:443>
    # Server Name Indication for SSL named virtual hosts is currently not
    # supported by MSIE on Windows XP.
    Listen 443
</IfModule>
<IfModule mod_gnutls.c>
    Listen 443
</IfModule>

任何帮助将不胜感激!

终于想通了...不是我的重写规则不起作用,而是与我使用的代理(cloudflare)发生冲突。对于那些处于类似情况的人,您必须在cloudflare仪表板中创建"页面规则",否则它将忽略您的Apache规则。

最新更新