【问题标题】:Symfony 4 + Apache 2.4 + mod_rewrite not workingSymfony 4 + Apache 2.4 + mod_rewrite 不工作
【发布时间】:2018-09-14 18:54:32
【问题描述】:

我做错了什么?

这是我在 /etc/apache2/sites-enabled/hello-world.conf 上的配置

<VirtualHost *:80>
    ServerName hello-world
    DocumentRoot "/home/carlos/dev/github/hello-world/public"

<Directory "/home/carlos/dev/github/hello-world/public">
    AllowOverride None
    Order Allow,Deny
    Allow from All

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

# uncomment the following lines if you install assets as symlinks
# or run into problems when compiling LESS/Sass/CoffeeScript assets
# <Directory /var/www/project>
#     Options FollowSymlinks
# </Directory>

# optionally disable the RewriteEngine for the asset directories
# which will allow apache to simply reply with a 404 when files are
# not found instead of passing the request into the full symfony stack
<Directory /var/www/project/public/bundles>
    <IfModule mod_rewrite.c>
        RewriteEngine Off
    </IfModule>
</Directory>

    ErrorLog /var/log/apache2/project_error.log
    CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>

当我尝试访问 http://hello-world/index.php/lucky/number 时,一切正常。

但是使用 http://hello-world/lucky/number 我得到一个 404 Not Found。

404 错误是由于我配置了两个 VirtualHost。一个在 /etc/apache2/sites-enabled/000-default.conf 中,另一个在 /etc/apache2/sites-enabled/hello-world.conf 中。我在 000-default.conf 中删除了它,现在我收到“500 内部服务器错误”。我改变了这一行:

<Directory "/home/carlos/dev/github/hello-world/public"> 
# AllowOverride None 
# Order Allow,Deny 
Require all granted 
# Allow from All – 

...但我仍然得到 500。

在 /var/log/apache2/project_error.log 上有这条消息:

[Thu Apr 05 19:56:25.819680 2018] [core:error] [pid 4277] [client 127.0.0.1:38800] AH00125: Request exceeded the limit of 10 subrequest nesting levels due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

【问题讨论】:

  • 你的操作系统是什么?

标签: php .htaccess symfony mod-rewrite apache2.4


【解决方案1】:

好的,我终于明白了:

这是一个错误的配置:

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

我在关注这里所说的内容:https://httpd.apache.org/docs/2.4/rewrite/remapping.html

但后来我尝试不使用这个 FallbackResource,回到 Symfony 文档给我的东西,用这个:

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

现在它正在工作。

谢谢大家

【讨论】:

    【解决方案2】:

    这可能是由于 Apache 2.4.18 之前的版本中的一个错误引起的。

    在主页上调用 FallbackResource 时存在 Apache 错误,导致 Apache 挂起。

    Apache 错误详情可以在这里找到:https://bz.apache.org/bugzilla/show_bug.cgi?id=58292

    I've added a PR to update the Symfony docs

    【讨论】:

      【解决方案3】:

      如果您使用的是 apache 2.4,请启用 mod_rewrite,并在您的虚拟主机错误日志文件中获得如下内容:

      [Thu Apr 05 15:08:55.619587 2018] [authz_core:error] [pid 2382] [client 10.0.1.1:51436] AH01630:客户端被服务器配置拒绝:/vagrant/public/

      然后更新您的虚拟主机并替换:

      订单允许,拒绝

      全部允许

      要求全部授予

      还要确保将 /var/www/project/public/bundles 更新为您的实际目录。

      【讨论】:

        猜你喜欢
        • 2016-03-31
        • 1970-01-01
        • 2015-01-14
        • 1970-01-01
        • 2012-12-19
        • 2016-05-09
        • 2016-01-19
        • 1970-01-01
        • 2015-09-22
        相关资源
        最近更新 更多