如何使用 mod_write 从 url 中删除 index.php,同时仍然允许 php 看到其中包含 index.php 的路径?

2024-04-17

在过去的三天里,我一直在使用 Apache 的 mod_rewrite 试图让它从我的 url 中删除 index.php,而 php 仍然需要在路径中看到它。

Essentially PHP needs to see this
http://example.com/index.php/Page/Param1/Param2

While the user needs to see this
http://example.com/Page/Param1/Param2

我现在拥有的是 htaccess 文件中的以下内容

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]

这是从另一页获取的,接近我需要的内容。然而,这似乎切断了一切之后http://example.com/部分。我怎样才能让 mod_rewrite 向用户显示一件事并让 php 看到其他东西?


这是修改后的代码,您可以在 .htaccess(在 DOCUMENT_ROOT 下)中使用它来从 URI 中删除 index.php:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (?!^index\.php)^(.+)$ /index.php/$1 [L,NC]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php(/[^\s\?]+)? [NC]
RewriteRule ^ %1%2 [R=302,L]

一旦您确信 R=302 工作正常,请将其更改为 R=301。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 mod_write 从 url 中删除 index.php,同时仍然允许 php 看到其中包含 index.php 的路径? 的相关文章

随机推荐