可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Consider the following URL : http://www.myurl.fr/accueil.
It won't work. However http://www.myrurl.fr/app.php/accueil does work.
I got rid of the .htaccess file because I want to use the vhost file and rely on Apache routing. My vhost file is as follow:
<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've cleared Apache cache and restarted it many times. The urlrewrite mod is enabled. I just don't know what else to check. Any idea what I'm missing ?
EDIT
It could be that both URL won't work at a given time since I'm working on it. My issue is still valid.
回答1:
Compared your rules with symfony-standard .htaccess, I saw that you missed file checking before 'pass everything rule'. Try to
<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:
The Symfony docs offer this straight forward 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 the following consideration.
If you are working with Apache you must be sure to enable mod_rewrite!
`sudo a2enmod rewrite`
So if you're beating your head against the wall, wondering why nothing is working, try this. It just might save your sanity and your project! Happy coding!
回答3:
change the apache httpd.conf virtual host code to this
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>
回答4:
The Key solution here is to make sure that mod_rewrite is enabled by typing
a2enmod rewrite
If you have apache 2.4, then check your config file that have instead of Order allow.deny
Require all granted
Here is a sample config inside directory directive
AllowOverride all
Require all granted
回答5:
@icarlosmendez Your suggestion of "sudo a2enmod rewrite" really saves my day!
This is what I did, hope some people would find it helpful
- run "sudo a2enmod rewrite" first
- copy the code from symfony, place it under "/etc/apache2/sites-available"
- just a note to people who got 500 errors, check that var under project folder got 777.
回答6:
Add this code to your apache conf
/etc/apache2/sites-available/000-default.conf
and make sure that 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>