3

I want to be able to access my symfony2 app with two URL's, without changing the server configuration. I tried to accomplish this by using mod_rewrite, in my case I want to be able to access my application at http://example.com/ and http://example.com/test/

The .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, line 3 and 4 are from the symfony2 manual. The reponse is a 404 from symfony. Now my questions are:

1) Is it possible to do this using mod_rewrite?
2) If not, what would be the best solution?

liecno
  • 924
  • 1
  • 8
  • 18

1 Answers1

0

One option seems to be to create a folder named "test" and give it it's own copy of app_dev.php and .htaccess

You may need to tweak the last line. The default .htaccess file doesn't use %{DOCUMENT_ROOT} but my setup 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 an extra "../" to the require statements.

A quick test on one of my projects shows that this seems to work.

Update

I don't believe this is possible simply with rewrite rules. This is my attempt at modifying my web/.htaccess file to work. My understanding is that if you request /test/foo, the URL is rewritten to internally become /app_dev.php/foo. However the way Symfony finds the URL that it tries to route somehow always finds out 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 does redirect the browser to /app_dev.php/foo and gives me the intended page. Since it doesn't hide the app_dev.php it's probably not what you're looking for, but it verifies that the match is working correctly.

Asa Ayers
  • 4,854
  • 6
  • 40
  • 57