php/symfony2 hiding app.php from the URL

倾然丶 夕夏残阳落幕 提交于 2019-12-03 00:32:29

Compared your rules with symfony-standard .htaccess, I saw that you missed file checking before 'pass everything rule'. Try to

<IfModule mod_rewrite.c>
    RewriteEngine On  

    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
    # If the requested filename exists, simply serve it.
    # We only want to let Apache serve files and not directories.
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    # Rewrite all other queries to the front controller.
    RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>

The Symfony docs offer this straight forward solution to this problem.

'<VirtualHost *:80>
    ServerName domain.tld
    ServerAlias www.domain.tld

    DocumentRoot /var/www/project/web
    <Directory /var/www/project/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>

    # uncomment the following lines if you install assets as symlinks
    # or run into problems when compiling LESS/Sass/CoffeScript 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/web/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>'

One thing I would like to add to this discussion is that none of these excellent suggestions will bear fruit without the following consideration.

If you are working with Apache you must be sure to enable mod_rewrite!

`sudo a2enmod rewrite`

So if you're beating your head against the wall, wondering why nothing is working, try this. It just might save your sanity and your project! Happy coding!

Sofien Benrhouma

change the apache httpd.conf virtual host code to this

ServerName my-site.fr
<VirtualHost your.ip.address:80>
DocumentRoot /var/www/mysite/web
<Directory "/var/www/mysite/web">
DirectoryIndex app.php
Options -Indexes FollowSymLinks SymLinksifOwnerMatch
AllowOverride All
Allow from All
</Directory>
</VirtualHost>

The Key solution here is to make sure that mod_rewrite is enabled by typing

a2enmod rewrite

If you have apache 2.4, then check your config file that have instead of Order allow.deny

Require all granted

Here is a sample config inside directory directive

AllowOverride all
Require all granted

@icarlosmendez Your suggestion of "sudo a2enmod rewrite" really saves my day!

This is what I did, hope some people would find it helpful

  1. run "sudo a2enmod rewrite" first
  2. copy the code from symfony, place it under "/etc/apache2/sites-available"
  3. just a note to people who got 500 errors, check that var under project folder got 777.

Add this code to your apache conf /etc/apache2/sites-available/000-default.conf

and make sure that mod_rewrite is enabled by typing

a2enmod rewrite

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/web/
    DocumentRoot /var/www/html/web
    <Directory /var/www/html/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>
</VirtualHost>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!