Symfony3强制https全部生成错误无限循环

时间:2018-05-30 23:33:20

标签: .htaccess symfony https

我在Symfony3中建立了网站。我想强制所有路线https。

我将此添加到我的.htaccess文件中:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

主页工作正常,用户重定向到https的版本但是在链接为char的任何子页面上“/”我有问题“错误:无限循环”

例如,这项工作很好:

https://www.example.com

https://www.example.com/subpage1

但这会生成错误无限循环: https://www.example.com/subpage1/subpage2

当我在.htaccess中删除强制https时,一切正常......

1 个答案:

答案 0 :(得分:0)

避免使用.htaccess来做这些事情 除非您计划仅在https中拥有站点的某些部分,否则请使用vhost配置文件。

以下是使用Apache 2的Symfony 3的完整示例

<VirtualHost *:80>
    ServerName www.example.com

    Redirect / https://www.example.com
</VirtualHost>

<VirtualHost _default_:443>
    ServerName www.example.com

    DocumentRoot /var/www/www.example.com/web
    <Directory /var/www/www.example.com/web>
        AllowOverride None
        Order Allow,Deny
        Allow from All

        <IfModule mod_rewrite.c>
            Options -MultiViews
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ app.php [QSA,L]
        </IfModule>
    </Directory>

    <Directory /var/www/www.example.com/web/bundles>
        <IfModule mod_rewrite.c>
            RewriteEngine Off
        </IfModule>
    </Directory>

    SSLCertificateFile /etc/letsencrypt/live/www.example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf

    ErrorLog /var/www/www.example.com/var/logs/error.log
    CustomLog /var/www/www.example.com/var/logs/access.log combined
</VirtualHost>

来源:https://wiki.apache.org/httpd/RedirectSSL