Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RewriteRule in Apache with Symfony2 not removing app.php

I have the following .htaccess file in my web directory for my Symfony2 installation:

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

However, when I try something basic such as:

(whatever)/web/app.php/place

it doesn't removing the app.php from the URL. I'm new to Apache and I'm not sure what's going on here, can anyone shed some light? Thanks in advance!

EDIT: Due to the structure of the web app I am working with I cannot move app.php or app_dev.php outside of the web folder nor can I modify the server configuration of Apache in anyway, thus I am looking for an alternative solution to this problem.

like image 400
celestialorb Avatar asked Dec 08 '11 00:12

celestialorb


4 Answers

You must enable the rewrite module in Apache, as the "IfModule mod_rewrite.c" states.

To do so:

  1. Run command-line command "a2enmod rewrite".
  2. Change all "AllowOverride None" lines to "AllowOverride All".

See http://www.lavluda.com/2007/07/15/how-to-enable-mod_rewrite-in-apache22-debian/.

like image 167
Pat Zabawa Avatar answered Nov 16 '22 07:11

Pat Zabawa


That rewrite rule is not meant to remove app.php from the URL. It's purpose is to use app.php for every request, if a request URL doesn't correspond to a real file. Since app.php is a real file, it will be used to serve a request.

If you want get rid of web/app.php part, first create a virtual host pointing to the web folder:

<VirtualHost *:80>
    ServerName whatever
    DocumentRoot /path/to/project/web
</VirtualHost>

This will remove the web part.

Then, to remove the app.php part, add this to the beginning of the web/.htaccess file:

RedirectMatch permanent ^/app\.php/(.*) /$1
like image 38
Elnur Abdurrakhimov Avatar answered Nov 16 '22 05:11

Elnur Abdurrakhimov


I just got the same issue and I fixed it like this:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /web/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
like image 4
gimpe Avatar answered Nov 16 '22 05:11

gimpe


I had this problem today and the fix was to append a /$1 at the end of the url rewrite rule.

My .htaccess looks like this:

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

And the virtual host is defines as this:

<VirtualHost *:80>
   DocumentRoot /var/www/html/symfony_site/web
   DirectoryIndex app.php
   <Directory /var/www/html/symfony_site/web >
       AllowOverride All
       Allow from All
   </Directory>
</VirtualHost>
like image 4
Marin Avatar answered Nov 16 '22 05:11

Marin