考虑以下URL:http://www.myurl.fr/accueil.
这行不通。然而http://www.myrurl.fr/app.php/accueil确实有效。
我去掉了.htaccess文件,因为我想使用vhost文件并依赖Apache路由。我的vhost文件如下:
<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>
我已经清除了Apache缓存并多次重新启动它。urlrewrite mod已启用。我只是不知道还能查什么。知道我错过了什么吗?
编辑这可能是因为两个URL在给定的时间都不起作用,因为我正在处理它。我的问题仍然有效。
将您的规则与symfony standard.htaccess进行比较,我发现您错过了"传递所有规则"之前的文件检查。尝试
<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>
Symfony文档为这个问题提供了直接的解决方案。
'<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>'
我想在这次讨论中补充的一点是,如果不进行以下考虑,这些出色的建议都不会取得成果。
如果您正在使用Apache,则必须确保启用mod_rewrite!
`sudo a2enmod rewrite`
所以,如果你的头撞在墙上,想知道为什么什么都不起作用,试试这个。这可能会拯救你的理智和你的项目!编码快乐!
将apachehttpd.conf虚拟主机代码更改为这个
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>
这里的关键解决方案是确保通过键入启用mod_rewrite
a2enmod rewrite
如果您有apache 2.4,请检查您的配置文件,该文件具有而不是Order allow.deny
Require all granted
以下是目录指令中的配置示例
AllowOverride all
Require all granted
@icarlosmendez你关于"sudo a2enmod rewrite"的建议真的拯救了我的一天!
这就是我所做的,希望有些人会发现它对有帮助
- 首先运行"sudo a2enmod rewrite"
- 从symfony复制代码,将其放在"/etc/apache2/sites available"下
- 只是给那些得到500个错误的人的一个提示,检查一下项目文件夹下的var得到了777
将此代码添加到您的apache conf/etc/apache2/站点可用/000 default.conf
并通过键入确保mod_rewrite已启用
a2enmod重写
<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>