Categories
symfony

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>