Jan 08
Digg
Stumbleupon
Technorati
Delicious

How to hide symfony project in subfolder

As for me, this is gonna be quite popular task – do not use symfony project in root of web folder but keep it under kinda “forum” or “users” folder. This way you may use in root of web folder (public_html or web or wherever you have called it) another type of software (e.g. Joomla). So for instance when you go to domain.com you see Joomla pages and when you go to domain.com/forum you will get access to symfony-driven project.

This is not something complecated but requires some tuning. So first of all you’ll need to change a line in your .htaccess file in root: RewriteRule ^(.*)$ /forum/index.php [QSA,L]
This way your .htaccess (which is located directly in public_html) has to look like this one:

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 ^(.*)$ /forum/index.php [QSA,L]

# big crash from our front web controller
ErrorDocument 500 “Application error. symfony application failed to start properly”

And now of course you’ll need to move you symfony project into forum directory and change a bit your index.php, frontend_dev.php, backend.php, etc controller files so they contain correct path to symfony root:

define(‘SF_ROOT_DIR’, realpath(dirname(__FILE__).’/../../’));

and you may also want to have fixed trailing slash problem on some hostings so use the following line for it (index.php is of course for index.php file and the other ones has to contain correspondent filename):

$_SERVER[‘SCRIPT_NAME’]=’/forum/index.php’;

That’s it. In your forum directory you’ll have to keep standard .htaccess:

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]

# big crash from our front web controller
ErrorDocument 500 “Application error. symfony application failed to start properly”

Of course this is not that necessary to hide your project into a folder if you can have a workaround with routing features of symfony. But sometimes technique which I’ve just described can be very-very helpfull.

See ya!


Author: symfonian

5 Comments

pedro
January 9, 2008

I didnt test it, but if i use image_tag(“foo”).

Is it gonna get mydomain.com/forum/images/foo.png?

There is any problem with the absolute paths? Because symfony always get mydomain.com/ root folder. So, thats why you change define(тАЩSF_ROOT_DIRтАЩ, realpath(dirname(__FILE__).тАЩ/../../тАЩ)); ?

take a look at symfonybr.com. thats my new blog about symfony.

tks

[…] How to hide symfony project in subfolder […]

[…] How to hide symfony project in subfolder […]

Lost
February 23, 2010

Doesnt seems to be working.

domain.com/forum gives 400 Bad Request

domain.com/forum/ loads OK

Lost
February 27, 2010

how can we get it to append /forum/ to the URL displayed? as it stays at domain.com, however links are correct with domain.com/forum/list/2 etc.

thanks

Comments RSS TrackBack Identifier URI

Leave a comment