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

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

有帮助吗?

解决方案

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

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

但是您应该从一开始就最好使用正确的URL /index.php.

其他提示

如果你想全局重写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 引用 并将重写工作留给网络服务器。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top