我想在查询参数后从我的URL中删除index.php
。
这是我的网址:
http://127.0.0.1/user/report?user=USERNAME
我已删除查询参数,并使用以下命令将其转换为漂亮的URL:
RewriteCond %{QUERY_STRING} !user=
RewriteRule ^([a-zA-Z0-9\-]+)/(.*)$ $2?user=$1 [L,QSA]
RewriteRule ^([a-zA-Z0-9\-]+)$ ?user=$1 [L,QSA]
现在,我的网址看起来像这样:
http://127.0.0.1/user/report/USERNAME
因此,对此URL的所有请求都将指向我项目的输入脚本,即web/index.php
。
当我使用以下路线获取数据时,它起作用:
http://127.0.0.1/user/report/Default/index.php/api/registration/user-registrations/
但是当我从URL中删除index.php
并按如下方式访问它时,它会抛出404:
http://127.0.0.1/user/report/Default/api/registration/user-registrations/
我正在使用Symfony路由我的所有路由。
[如果您使用的是Apache 2.4,并且不想使用.htaccess
文件(例如,出于性能原因),那么解决方案就是使用oneliner:FallBackResource
。
您只需要这个:
FallBackResource
甚至在<VirtualHost *:80>
ServerName domain.tld
ServerAlias www.domain.tld
DocumentRoot /var/www/project/public
DirectoryIndex /index.php
<Directory /var/www/project/public>
AllowOverride None
Order Allow,Deny
Allow from All
FallbackResource /index.php
</Directory>
# optionally disable the fallback resource for the asset directories
# which will allow Apache to return a 404 error when files are
# not found instead of passing the request to Symfony
<Directory /var/www/project/public/bundles>
FallbackResource disabled
</Directory>
</VirtualHost>
中也显示。