Php / symfony2 hides app.php from url

Consider the following URL: http://www.myurl.fr/accueil .

This will not work. However, http://www.myrurl.fr/app.php/accueil does work.

I got rid of the .htaccess file because I want to use a vhost file and rely on Apache routing. My vhost file is as follows:

<VirtualHost my.ip.address> ServerName myurl.fr ServerAlias www.myurl.fr DocumentRoot /var/www/mysite/web DirectoryIndex app.php <Directory "/var/www/mysite/web"> AllowOverride All Allow from All </Directory> <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteRule ^app\.php(/(.*)|$) %{CONTEXT_PREFIX}/$2 [R=301,L] RewriteRule .? - [L] RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^(.*)$ app.php [QSA,L] RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$ RewriteRule ^(.*) - [E=BASE:%1] RewriteRule .? %{ENV:BASE}app.php [L] </IfModule> </VirtualHost> 

I cleared the Apache cache and restarted it many times. Urlrewrite modification is included. I just don't know what else to check. Any idea what I am missing?

EDIT Perhaps both URLs will not work at a specific time, since I am working on it. My problem is still relevant.

+6
source share
6 answers

Comparing my rules with symfony-standard .htaccess , I saw that you skipped checking files before "passing all the rules." Try

 <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> 
+6
source

Symfony docs offer this direct solution to this problem.

 '<VirtualHost *:80> ServerName domain.tld ServerAlias www.domain.tld DocumentRoot /var/www/project/web <Directory /var/www/project/web> AllowOverride None Order Allow,Deny Allow from All <IfModule mod_rewrite.c> Options -MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ app.php [QSA,L] </IfModule> </Directory> # uncomment the following lines if you install assets as symlinks # or run into problems when compiling LESS/Sass/CoffeScript assets # <Directory /var/www/project> # Options FollowSymlinks # </Directory> # optionally disable the RewriteEngine for the asset directories # which will allow apache to simply reply with a 404 when files are # not found instead of passing the request into the full symfony stack <Directory /var/www/project/web/bundles> <IfModule mod_rewrite.c> RewriteEngine Off </IfModule> </Directory> ErrorLog /var/log/apache2/project_error.log CustomLog /var/log/apache2/project_access.log combined </VirtualHost>' 

One thing I would like to add to this discussion is that none of these excellent suggestions will bear fruit without further consideration.

If you are working with Apache, you should definitely enable mod_rewrite!

 `sudo a2enmod rewrite` 

So, if you bang your head against a wall, wonder why nothing works, try this. It can save your sanity and your project! Happy coding!

+4
source

change apache httpd.conf virtual host code to

 ServerName my-site.fr <VirtualHost your.ip.address:80> DocumentRoot /var/www/mysite/web <Directory "/var/www/mysite/web"> DirectoryIndex app.php Options -Indexes FollowSymLinks SymLinksifOwnerMatch AllowOverride All Allow from All </Directory> </VirtualHost> 
+3
source

The key solution here is to make sure mod_rewrite is enabled by typing

 a2enmod rewrite 

If you have apache 2.4 , check your configuration file instead of Order allow.deny

 Require all granted 

Here is an example configuration inside a directory directive

 AllowOverride all Require all granted 
+2
source

@icarlosmendez Your suggestion "sudo a2enmod rewrite" really saves my day!

This is what I did, hope some people find it useful.

  • run "sudo a2enmod rewrite" first
  • copy the code from symfony , place it under "/ etc / apache2 / sites-available"
  • just notice to people who got 500 errors, make sure var in the project folder got 777.
0
source

Add this code to your apache conf /etc/apache2/sites-available/000-default.conf

and make sure mod_rewrite is enabled by typing

a2enmod rewrite

 <VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/html/web/ DocumentRoot /var/www/html/web <Directory /var/www/html/web> AllowOverride None Order Allow,Deny Allow from All <IfModule mod_rewrite.c> Options -MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ app.php [QSA,L] </IfModule> </Directory> </VirtualHost> 
0
source

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


All Articles