【问题标题】:htaccess help, removing a string from the URLhtaccess 帮助,从 URL 中删除一个字符串
【发布时间】:2011-04-23 17:29:40
【问题描述】:
【问题讨论】:
标签:
apache
.htaccess
mod-rewrite
server-configuration
【解决方案1】:
是的,你可以。使用此规则,任何请求的 /index.php 都将被删除:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php[/?\ ]
RewriteRule ^index\.php(/(.*))?$ /$2 [L,R=301]
但您最好从一开始就使用正确的 URL,以便您的应用程序提供链接不包含 /index.php 的文档。
【解决方案2】:
如果要全局重写 index.php/controller/action
这个 .htaccess 配置应该可以解决问题:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
此配置在 Apache 上检查文件/目录是否存在于磁盘上(即请求与磁盘上的真实资源匹配),并在需要时将请求重写为 your front controller。
所以http://www.domain.com/resources/image.png 应该返回图片资源。
而http://www.domain.com/user/show/5 应该透明地重写为http://www.domain.com/index.php/user/show/5
使用此配置,您可以删除应用程序 URL 中的所有 index.php 引用,并将重写留给 Web 服务器。