Symfony on virtual host (document root problem)

2020-02-29 08:10发布

I'm developing an application in Symfony and on localhost (XAMPP) I want to simulate the same conditions as on the webserver.

The web server is configured as follows:

/www   => mydomain.com
/foo   => foo.mydomain.com
/bar   => bar.mydomain.com
...

I'm going to put my Symfony application into /www direcotry so there'll be:

/www
/www/apps
/www/apps/frontend
/www/apps/frontend/...
/www/apps/backend
/www/apps/backend/...
/www/cache
/www/config
... and so on...
/www/web

The thing is that the document root is still set to the /www directory but Symfony expects it in the /www/web.
Of course it will work if I call http://mydomain.com/web but I guess you understand this is quiet stupid solution.

So my question is: Is there any way how can I change/bypass the default document root setting using .htaccess or whatever?

EDIT: I solved it.

3条回答
你好瞎i
2楼-- · 2020-02-29 08:30

I you got hand on apache config, the better is to move document root from /www to /www/web in you virtualHost config and allow php (if restricted by open_basedir configuration directive) to access the whole /www directory.

查看更多
来,给爷笑一个
3楼-- · 2020-02-29 08:42

Change the way symfony expects your directory to be named :

// config/ProjectConfiguration.class.php
class ProjectConfiguration extends sfProjectConfiguration
{
  public function setup()
  {
    $this->setWebDir($this->getRootDir().'/www/or/whatever/directory');
  }
}
查看更多
Emotional °昔
4楼-- · 2020-02-29 08:48

I don't know much about Symfony but /web is supposed to be the document root. All the other directories should be outside the document root for security reasons for one, and to avoid the /web part in the URL for another. But it looks like you already know that.

If you can edit the web server's configuration, try to reflect that and set DocumentRoot to the web directory.

If you can't do that: It's not possible to change the DocumentRoot in a .htaccess file but it is possible to rewrite all requests so they go to /web internally using mod_rewrite. It's kludgy, but probably the best solution if you can't influence DocumentRoot.

I'm not a mod_rewrite guru so I can't provide an example (I would have to test it first and I can't do that right now) but I'm sure somebody will. Maybe add the mod_rewrite tag to your question.

Update: Untested but should work. Put into a .htaccess file in /www:

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_URI} !^/web/ 
RewriteRule .* /web/%1 [QSA]

you would then need to change your configuration files to use http://www.domain.com/ instead of http://www.domain.com/web/ of course.

I can't say whether that interferes with any other .htaccess rules on the Symfony end - you'd have to try out.

查看更多
登录 后发表回答