Oct 30
Digg
Stumbleupon
Technorati
Delicious

Tricks with symfony .htaccess

I decided to put here a couple of .htaccess rules that may help you in your daily practice to save the time trying to get it working. Real .htaccess guru of course wont find it usefull but maybe someone will tell me thank you :-)

How to use Symfony on naked IP without domain name

Sometimes you dont have domain name but you would like to get your symfony project working. All you need is love, love, love and add the following line into your .htaccess

RewriteRule ^(.*)$ http://XX.XX.XX.XX/~accountname/index.php [QSA,L]

so full .htacces must look like this:

<IfModule mod_rewrite.c>
RewriteEngine On

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

# we skip all files with .something
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]
RewriteRule ^(.*)$ http://XX.XX.XX.XX/~accountname/index.php [QSA,L]
</IfModule>

How to get visitors redirected to WWW.

Sometimes you dont have domain name but you would like to get your symfony project working. All you need is love, love, love and add the following line into your .htaccess

RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

so full code must look like:

<IfModule mod_rewrite.c>
RewriteEngine On

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

RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

# we skip all files with .something
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]
</IfModule>

and the last but not least thing

How to use Symfony along with the other software in the same directory

This one is a bit more tricky that the other ones but also nothing complicated. I think you may found this or similar information on symfony forum but here I collected what I have been really tried and made sure it’s working. So lets consider a using of popular CMS Wordpress along with Symfony project. So because of WP uses index.php file as a core we would need to rename symfony’s index.php file into somthing else, lets name it symfony-index.php and next step it to add the following code into .htaccess:

RewriteRule ^/blog/wp-admin/(.*)$ /blog/wp-admin/index.php [L]
RewriteRule ^/blog/(.*)$ index.php [L]
RewriteRule . /index.php [L]
RewriteRule ^(.*)$ symfony-index.php [QSA,L]

Full .htaccess must look like this:

<IfModule mod_rewrite.c>
RewriteEngine On

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

# we skip all files with .something
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 ^/blog/wp-admin/(.*)$ /blog/wp-admin/index.php [L]
RewriteRule ^/blog/(.*)$ index.php [L]
RewriteRule . /index.php [L]
RewriteRule ^(.*)$ symfony-index.php [QSA,L]

</IfModule>


Author: admin

8 Comments

Dave
October 30, 2007

Thx for that!

Really helpful. Writing .htaccess files can really be a pain.

Thierry Schellenbach
October 31, 2007

Indeed these things can be a pain if you don’t have experience with them. Especially query string rewriting is a pain.

Good post, think it will help quite a few people.

[…] Tricks with symfony .htaccess […]

[…] Tricks with symfony .htaccess […]

Junni
February 12, 2008

Great! Just needed the latest htaccess trick to integrate a standard Wordpress blog into my Sf website

Jagtesh Chadha
June 4, 2008

Brilliant! That’s a really neat work-around.

Alex Filatov
June 19, 2008

Thanks dude! :)
I’ve found also that if we don’t add L directive to RewriteRule (right near R=301) we’ll get appended index.php in address bar.
Be informed! :0)

Sraboni
July 2, 2008

Comments RSS TrackBack Identifier URI

Leave a comment