0

我想知道,是否可以从 URL 中删除 index.php?基本上在网站的某些页面上,我有这种结构,

http://www.domain.com/index.php/members/register,但其他页面我有这样的 URL 结构,http://www.domain.com/category/products/id/5,我想知道htaccess 是否可以在需要时删除 index.php 和任何属性斜杠?我该怎么做呢?

4

2 回答 2

0

如果要全局重写 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 上检查文件/目录是否存在于磁盘上(即请求与磁盘上的真实资源匹配),并在需要时将请求重写到您的前端控制器

所以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 服务器。

于 2010-10-11T10:15:14.683 回答
0

是的你可以。使用此规则,任何请求/index.php都将被删除:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php[/?\ ]
RewriteRule ^index\.php(/(.*))?$ /$2 [L,R=301]

但是您最好从一开始就使用正确的 URL,以便您的应用程序提供链接不包含/index.php.

于 2010-10-11T16:53:42.893 回答