Symfony 4 + Apache 2.4 + mod_rewrite not working

chuckedw picture chuckedw · Apr 5, 2018 · Viewed 8.2k times · Source

What am I doing wrong?

This is my config on /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>

When I try to access http://hello-world/index.php/lucky/number all goes right.

But with http://hello-world/lucky/number I get a 404 Not Found.

The 404 error was due I had configure two VirtualHosts. One in /etc/apache2/sites-enabled/000-default.conf and other in /etc/apache2/sites-enabled/hello-world.conf. I removed that in 000-default.conf, and now I'm receiving a "500 Internal Server Error". I change this lines:

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

...but still I'm getting the 500.

On /var/log/apache2/project_error.log there's this message:

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

Answer

chuckedw picture chuckedw · Apr 6, 2018

Ok, finally I got:

It was a misconfiguration here:

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

I was following what is said here: https://httpd.apache.org/docs/2.4/rewrite/remapping.html

But then I tried to do without this FallbackResource, back to what Symfony documentation gave me, with this:

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

Now it's working.

Thank you all