Symfony 2 project in a subdirectory

I need to deploy a Symfony 2 project in a subdirectory on a production server. Effectively, this would mean that all URLs have a prefix / subdirectory / path, i.e.

http://host.com/subdirectory/project/web/app.php/survey 

I do not need to rewrite the URLs, and I am not going to configure it. The application should work when accessed only through the above URL.

The problem is that all the links generated by the path and asset Twig functions refer to the server root (/), and not to the subdirectory where the project is located (/ subdirectory /). Is there any configuration option in Symfony 2 to override relative paths around the world?

I tried to work around this problem by adding an HTML tag, but it does not work for links.

Update: I owe you more. I am running IIS with its version of mod_rewrite, so some of your suggestions may be valid.

Update: Ideally, I would like to see a solution that sets the root directory to the AppKernel method object AppKernel::setRootDir() if it existed to complement the existing AppKernel::getRootDir() .

+4
source share
5 answers

Strange ... with the default configuration, the path / resource should handle the subdirectories. Have you tried it with Symfony distribution out of the box? On localhost, they mostly use it that way. Could you post your configuration file?

In any case ... For resource paths, you can try to configure:

 #config.yml templating: assets_base_urls: { http: "http://yoursite.com/dir1", ssl: "https://yoursite.com/dir1" } 

( http://symfony.com/doc/current/reference/configuration/framework.html#assets-base-urls )

For the router, you should read: http://symfony.com/doc/current/cookbook/console/sending_emails.html#configuring-the-request-context-globally This may give you some ideas on changing the context of the router, for example:

 # app/config/parameters.yml parameters: router.request_context.host: example.org router.request_context.scheme: http router.request_context.base_url: my/path 

In any case, the context is automatically set based on the request information. Try debugging:

 $context = $this->getContainer()->get('router')->getContext(); // you should be mainly interested in: $context->getBaseUrl(); 

You can easily create a listener class that manually sets your base url if this is not the case anyway:

 class RouterContextListener { protected $router; public function __construct(RouterInterface $router) { $this->router = $router; } public function onKernelRequest(Event $event) { $this->router->setBaseUrl('somedir'); } } 
+3
source

You are looking for a RewriteBase rule, add it to your .htaccess :

 <IfModule mod_rewrite.c> RewriteEngine On RewriteBase /subdirectory/project/web RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ app.php [QSA,L] </IfModule> 

Also look at the symfony-standard 2.3 .htaccess file, which may help

 <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$ RewriteRule ^(.*) - [E=BASE:%1] RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L] # If the requested filename exists, simply serve it. # We only want to let Apache serve files and not directories. RewriteCond %{REQUEST_FILENAME} -f RewriteRule .? - [L] # Rewrite all other queries to the front controller. RewriteRule .? %{ENV:BASE}/app.php [L] </IfModule> 
+2
source

Check out the /config/routing.yml application, in which you import routing from your packages. The entries in this file look something like this:

 somename: resource: "@YourBundle/Resources/config/routing.yml" prefix: / 

You can add your subdirectory to a prefix, for example prefix: /subdirectory Then all generated URLs will include subdirectory .

0
source

To enable the rewrite module, run "apache2 enable module rewrite":

 sudo a2enmod rewrite 

To apply the changes, you must restart the web server:

 sudo service apache2 restart 

VirtualHost should be like ServerName localhost.myproject.dev ServerAdmin webmaster @localhost

 DocumentRoot /var/www/project_path <Directory /var/www/project_path> Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ /app.php [QSA,L] </IfModule> </Directory> 

this parameter redirects your index.php, index.html or something else:

 Options Indexes FollowSymLinks MultiViews 

he will rewrite your htacces!

  <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ /app.php [QSA,L] </IfModule> 
0
source

My solution for this in a local environment was to add the following code to app/config/config.yml

 framework: assets: base_urls: - 'http://localhost/symfony_dir/' 

No need to rewrite!

0
source

Source: https://habr.com/ru/post/1501439/


All Articles