How to use mod_rewrite to use symfony2 with two urls

I want to be able to access my symfony2 application with two urls without changing the server config. I tried to accomplish this with mod_rewrite, in my case I want to be able to access my app at and

Htaccess file looks like this:

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


I added line number 2, lines 3 and 4 from the symfony2 manual. The answer is 404 from symfony. Now my questions are:

1) Can this be done with mod_rewrite?
2) If not, what would be the best solution?


source to share

1 answer

One option is to create a folder called "test" and give it your own copy of app_dev.php and .htaccess

You may need to customize the last line. The .htaccess file does not use% {DOCUMENT_ROOT} by default, but my installation requires it.

RewriteEngine On
RewriteOptions Inherit
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/test/app_dev.php [QSA,L]


Then you need to edit test / app_dev.php to add additional "../" to your require statements.

A quick test on one of my projects shows that this works.


I don't believe this is possible just with the rewrite rules. This is my attempt at changing my web / .htaccess file to work. I understand that if you request / test / foo the url is rewritten to internally become / app_dev.php / foo. However, how does Symfony find that the url it tries to route always finds the url was / test / foo and I get a route not found exception.

RewriteEngine On
RewriteOptions Inherit

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^test/(.*)$ %{DOCUMENT_ROOT}/app_dev.php/$1 [QSA,L]

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


If I add change my app_dev.php line to use [QSA, L, R = 301], it redirects the browser to / app_dev.php / foo and gives me the intended page. Since it doesn't hide the app_dev.php, it is probably not the one you are looking for, but it does check if the match is working correctly.



All Articles