1

在过去的 3 天里,我一直在玩 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 看到其他内容?

4

2 回答 2

1

这条规则:

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

需要看起来像这样:

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

另请注意,RewriteRule ^(.*)$ index.php?$1 [L,QSA]它不会创建一个看起来像这样的 URI /index.php/Page/Param1/Param2,它会创建一个看起来像这样的查询字符串:/index.php?Page/Param1/Param2。这根本不是您所说的 PHP 需要看到的。

于 2012-07-20T01:08:51.327 回答
1

这是您可以在 .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。

于 2012-07-20T03:39:01.160 回答