取出[R:Redirect]后,mod_rewrite无法正常工作

时间:2013-03-25 17:34:54

标签: apache .htaccess codeigniter mod-rewrite

我使用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]

这有什么问题,我还有更好的解决方案吗?

非常感谢!

1 个答案:

答案 0 :(得分:0)

我认为这是因为[R] forces an external redirect在这种情况下已经设置了基数并且路径被附加到重定向端点的基本URL。

因此,对于[R],对http://example.com/index.php/item/view/2的请求,/index.php/item/view/2将附加到http://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 /初始/再导向#1] ...
  [RID#100991828 /初始/再导向#2] ...
  ...