Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php/symfony2 hiding app.php from the URL

Consider the following URL : http://www.myurl.fr/accueil.

It won't work. However http://www.myrurl.fr/app.php/accueil does work.

I got rid of the .htaccess file because I want to use the vhost file and rely on Apache routing. My vhost file is as follow:

<VirtualHost my.ip.address>
ServerName myurl.fr
ServerAlias www.myurl.fr
DocumentRoot /var/www/mysite/web
DirectoryIndex app.php
<Directory "/var/www/mysite/web">
  AllowOverride All
  Allow from All
</Directory>

<IfModule mod_rewrite.c>
 RewriteEngine On  
 RewriteCond %{ENV:REDIRECT_STATUS} ^$  
 RewriteRule ^app\.php(/(.*)|$) %{CONTEXT_PREFIX}/$2 [R=301,L]
 RewriteRule .? - [L]
 RewriteCond %{REQUEST_FILENAME} -f
 RewriteRule ^(.*)$ app.php [QSA,L]
 RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
 RewriteRule ^(.*) - [E=BASE:%1]    
 RewriteRule .? %{ENV:BASE}app.php [L]
</IfModule>
</VirtualHost>

I've cleared Apache cache and restarted it many times. The urlrewrite mod is enabled. I just don't know what else to check. Any idea what I'm missing ?

EDIT It could be that both URL won't work at a given time since I'm working on it. My issue is still valid.

like image 450
Sam Avatar asked Aug 16 '13 20:08

Sam


3 Answers

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>
like image 66
Alexey B. Avatar answered Sep 20 '22 01:09

Alexey B.


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!

like image 32
icarlosmendez Avatar answered Sep 22 '22 01:09

icarlosmendez


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>
like image 29
Sofien Benrhouma Avatar answered Sep 21 '22 01:09

Sofien Benrhouma