celestialorb
celestialorb

Reputation: 1989

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.

Upvotes: 14

Views: 42937

Answers (7)

Marin
Marin

Reputation: 1308

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>

Upvotes: 4

Neeraj
Neeraj

Reputation: 657

I managed to remove web/app.php part on my shared server.

  1. First I moved app.php out of the web directory one step above thus my app.php on the root directory. Then I placed a .htaccess on the root directory
RewriteEngine on

RewriteBase /

RewriteRule ^css/(.*) web/css/$1

RewriteRule ^images/(.*) web/images/$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]

I used RewriteCond on css and images as they still lie inside /web/ directory so all request to css and images will be routed to web/css and web/images directories respectively.

It worked well for me.

Upvotes: 0

Pat Zabawa
Pat Zabawa

Reputation: 740

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

Upvotes: 25

gimpe
gimpe

Reputation: 1057

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>

Upvotes: 4

Elnur Abdurrakhimov
Elnur Abdurrakhimov

Reputation: 44831

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

Upvotes: 21

MicE
MicE

Reputation: 5118

We've been struggling with that as well. There are two solutions:

  1. set document root of your virtual host to point directly to the /web/ subfolder (as per Symfony2 guidance)
  2. move app.php and app_dev.php outside of the /web/ subfolder, then update .htaccess and include paths in both files accordingly (i.e. remove web/ to correct all paths after you've manually moved the files one folder above)

(Note: cannot provide examples from vanilla installation of S2, since symfony.com seems to be down at the moment.)

Upvotes: 0

Brigand
Brigand

Reputation: 86220

When you write RewriteCond %{REQUEST_FILENAME} !-f you say "Don't change real files... they should still be reachable." If you remove that, it should work.

The reason for it allowing that is some frameworks let you use http://site.tld/script.php/abc=123&def=456 style of links.

If you want it to allow filenames except app.php, you could add that as another RewriteCond.

Upvotes: 0

Related Questions