1

我使用 CodeIgniter 并试图重写 url 以实现对 SEO 友好。目的是...

http://localhost/FOR-SEO/item/view/id
-> to -> http://localhost/item/view/id

(只是想把 FOR-SEO 排除在外)

我尝试按如下方式添加 htaccess,

RewriteRule ^(.*)/item/view/(.*)$ /item/view/$2 [R,L]

所以变成了……

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteRule ^(.*)/item/view/(.*)$ /item/view/$2 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

它工作正常,但在我取出 [R] 后,它显示未找到 404 错误。

我试图检查重写日志,但是对于 [R] 和没有 [R],一切都是一样的

这是带有 [R] 的日志(工作)

[rid#1009868a0/initial] (1) [perdir /Applications/MAMP/htdocs/himaparn/] escaping http://localhost:8888/item/view/2 for redirect
[rid#1009868a0/initial] (1) [perdir /Applications/MAMP/htdocs/himaparn/] redirect to http://localhost:8888/item/view/2 [REDIRECT/302]
[rid#10098a8a0/initial] (1) [perdir /Applications/MAMP/htdocs/himaparn/] internal redirect with /index.php/item/view/2 [INTERNAL REDIRECT]
[rid#100984da8/initial/redir#1] (1) [perdir /Applications/MAMP/htdocs/himaparn/] pass through /Applications/MAMP/htdocs/himaparn/index.php
[rid#1009978a0/subreq] (1) [perdir /Applications/MAMP/htdocs/himaparn/] internal redirect with /index.php/item/view/2 [INTERNAL REDIRECT]

这是没有 [R] 的日志,(不工作)

[localhost/sid#100813108][rid#10098f8a0/initial] (1) [perdir /Applications/MAMP/htdocs/himaparn/] internal redirect with /item/view/2 [INTERNAL REDIRECT]
[localhost/sid#100813108][rid#100993a50/initial/redir#1] (1) [perdir /Applications/MAMP/htdocs/himaparn/] internal redirect with /index.php/item/view/2 [INTERNAL REDIRECT]
[localhost/sid#100813108][rid#100991828/initial/redir#2] (1) [perdir /Applications/MAMP/htdocs/himaparn/] pass through /Applications/MAMP/htdocs/himaparn/index.php
[localhost/sid#100813108][rid#1009978a0/subreq] (1) [perdir /Applications/MAMP/htdocs/himaparn/] internal redirect with /index.php/item/view/2 [INTERNAL REDIRECT]

这有什么问题,我有更好的解决方案来完成这项工作吗?

非常感谢!

4

1 回答 1

0

我认为这是因为[R] 强制进行外部重定向,在这种情况下,基础已经设置,并且路径被附加到重定向端点的基础 url。

因此,与,对,[R]的请求将被附加到离开你http://example.com/index.php/item/view/2/index.php/item/view/2http://example.com/http://example.com/index.php/item/view/2

如果没有,[R]您将获得内部重定向。这将附加/index.php/item/view/2到请求中。因此,请求http://example.com/index.php/item/view/2将使您获得http://example.com/index.php/item/view/2/index.php/item/view/2一个404.

您可以非常清楚地看到日志中的多个重定向:

...
[rid#100993a50/initial/redir#1]...
[rid#100991828/initial/redir#2]...
...

于 2013-03-25T17:51:08.007 回答