How to run a Symfony site without the .htaccess file

January 28th, 2009 | Tags: , ,

I recently wrote a tip about how to overcome issues when working with mod_rewrite, PHP_SELF, and configuration settings within the main server or virtual host context as is recommended in the Apache documentation on Per-directory Rewrites (scroll down to the notes). This post will explain the specific changes needed to do this for the Symfony framework.

The default Symfony .htaccess contains:

RewriteEngine On

# uncomment the following line, if you are having trouble
# getting no_script_name to work
#RewriteBase /

# we skip all files with .something
# comment the following 3 lines to allow periods in routes
RewriteCond %{REQUEST_URI} \..+$
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule .* - [L]

# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f

# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]

To get the rules to work directly in the main server or virtual host context we need to add a couple leading forward-slashes. Then to fix the Apache bug referenced in the aforementioned tip, the last rule gets the PT (pass through) flag. These rules are now be usable within the main server or virtual host context:

RewriteEngine On

# uncomment the following line, if you are having trouble
# getting no_script_name to work
#RewriteBase /

# we skip all files with .something
# comment the following 3 lines to allow periods in routes
RewriteCond %{REQUEST_URI} \..+$
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule .* - [L]

# we check if the .html version is here (caching)
RewriteRule ^/$ index.html [QSA]
RewriteRule ^/([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f

# no, so we redirect to our front web controller
RewriteRule ^(.*)$ /index.php [QSA,L,PT]

Note: Don’t forget to delete your .htaccess file and restart Apache for these configuration changes to take effect.

No comments yet.

TOP