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>

31 replies on “Tricks with symfony .htaccess”

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)

Hi, I am using symfony and want to redirect a user from http://mysite.com
to http://www.mysite.com. I can do this a couple ways from what it seems–1.) through the htaccess and 2.) through a filter. Is the filter going to be slower? I am a newbie and really struggle iwth making sense of the htaccess file so if I can avoid it I would be most comfortable.

Thanks so much admin I am using the .htaccess now and loving it. One small question, what is the point of skipping everything with .something? (Code below here: # we skip all files with .something)

This piece of code is giving me trouble because my site generates a URL with periods like this: http://tzanalytics.com/author/subcategoryshow/Economics/2/both/Highest+overall/1.3/10.0/-1/10.0/-1/10.0/-1/10/

I removed that rewrite rule and didn’t notice any problems but I was wondering whether I need it for some reason

Well I can say that this line is important:

RewriteCond %{REQUEST_URI} !\.html$

if you want to have a way to add static HTML pages without rendering them by symfony’s .htaccess. E.g you may want to keep index.html which says we are not ready for launch yet, please come later or so πŸ™‚

Sry, but this article is bad, long and bad. I used your code and my application didn’t work. If I have all symfony files in folder ‘sf_sandbox’ on server in catalog ‘public_html’ – I had to add to catalog ‘public_html’ file .htaccess. So I have in this catalog 1 file and 1 folder. My .htaccess file looks:
Options +FollowSymLinks +ExecCGI

RewriteEngine On

RewriteRule ^admin(.*)$ backend.php [QSA,L]
RewriteRule ^(.*)$ /sf_sandbox/web/$1 [QSA,L]

only that !! YOU SHOULDN’T WRITE:
Options +FollowSymLinks +ExecCGI

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]

because it is in file .htaccess in sf_sandbox\web.

In routing.yml in backend we must also add:
homepage:
#start – it is homepage module for backend
url: /admin
param: { module: start, action: index }

default_index:
url: /admin/:module
param: { action: index }

default:
url: /admin/:module/:action/*

at least 5 guys left a comment that article was usefull so I dont think it’s really bad πŸ™‚
Anyway thanks for your opinion!

Well you can always rename backend.php to admin.php so url will look like:

http://localhost/admin.php/login

which is pretty similar πŸ™‚

This approach should also work:

RewriteRule ^(admin/([^.]+))$ backend.php/$1 [QSA,L]
RewriteRule ^(admin/*)$ backend.php [QSA,L]

“http://localhost/admin.php/login” – oh yes – I have forgotten – I am rather new in symfony and in tutorial was ‘backend’ ;]

Your idea:
RewriteRule ^(admin/([^.]+))$ backend.php/$1 [QSA,L]
RewriteRule ^(admin/*)$ backend.php [QSA,L]

also doesn’t work πŸ™ I think it is because of:
<form action=”” method=”post”>

in signinSuccess.php. I have everywhere where I have form:
<form action=”” method=”POST”>

and then is ok. Only in signinSuccess is ‘@sf_guard_signin’. Can I change it ? How ?

It it is important, my routing.yml looks:
homepage:
url: /admin
param: { module: start, action: index }

default_index:
url: /admin/:module
param: { action: index }

default:
url: /admin/:module/:action/*

*In each form action i have:
/admin/xxx/yyy

only in signinSuccess.php I have in form action:
β€˜@sf_guard_signin’

Maybe I should change it on /admin/something – but what should be this ‘something’ ?

sure, it’s in settings.yml:

prod:
.settings:
no_script_name: on

I guess you have it off now

Thanks for this article! I think I’m on the right way but it doesn’t work. I need to exclude phpmyadmin inside the /web directory (on the test server, not production πŸ™‚ and hoped it would work like your example with wordpress – no luck πŸ™

here’s what I tried:

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

does anyone have an idea?

Is there a way to password protect the entire website with htacces.
I’ve included something like
AuthUserFile /usr/local/you/safedir/.htpasswd
AuthGroupFile /dev/null
AuthName EnterPassword
AuthType Basic

Before the statements and it seems to block syfmony from getting started up.

Leave a Reply

Your email address will not be published. Required fields are marked *