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”
Thx for that!
Really helpful. Writing .htaccess files can really be a pain.
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 […]
Great! Just needed the latest htaccess trick to integrate a standard WordPress blog into my Sf website
Brilliant! That’s a really neat work-around.
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)
how do I redirect
http://www.somesite.com/elle to
http://www.somesite.com/example.php
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.
yes I’m pretty sure htaccess is the fastest way
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!
I have one problem in backend – in login form – when I write bad login or password and I click submit I am redirect to:
http://localhost/backend.php/login
but I want see: http://localhost/ADMIN/login
is it possible ?
My .htaccess looks:
Options +FollowSymLinks +ExecCGI
RewriteEngine On
RewriteRule ^admin(.*)$ backend.php [QSA,L]
RewriteRule ^(.*)$ /sf_sandbox/web/$1 [QSA,L]
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’ ?
It works:
<form action=”” method=”post”>
π
*/admin/login
Hello…I have a question…how can i do to have a url without index.php (www.url.com/index.php/module/action)
I whant to have only: http://www.url.com/module/action.
Can i do this without edit htacces
sure, it’s in settings.yml:
prod:
.settings:
no_script_name: on
I guess you have it off now
Hi,
I had the same issue than you all, and not that obvious to solve it on a clean way…
I made a how-to on particul.es blog.
If anyone interested : http://particul.es/blog/index.php?post/How-to-change-backend.php-to-admin-prefix-routes-automatically-symfony
[…] 1. Tricks with symfony .htaccess http://www.symfonylab.com/tricks-with-symfony-htaccess/ […]
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?
Hi Guys,
I am new in symfony i am stuck in .htaccces rules :(.. can some buddy help me with this.
Current URL
http://example.com/songs?title=jack&Where=pak&ple=10&pn=1
i want create a .htaccess rule so my url should look like
http://example.com/songs/jack/pak/10/1
ANy help on this :(…..
I Have an issue in symfony .htacess . I want to redirect my site to index.php like in this way
if a user click on
http://domain.com/app or http://domain.com/app/xxx/yyy {which is not valid}
it will redirect to
http://domain.com
Please help:
Indeed these things can be a pain if you donβt have experience with them. particularly query string rewriting is a pain
you have use very nice content in your post, thanks for share this nice content on this site,
hi.
first tnx for your content.
i test it but my problem do not solve
my .htaccess file is MYPROJECT/WEB
is it in the write place??
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.