1

我已经有一个使用 CodeIgniter 1.7 的客户网站大约 6 年了,没有任何问题。但是,最近我将 CodeIgniter 框架更新到 2.1.1,现在 .htaccess 出现了问题。我已经在日志中确认它没有到达我的控制器,所以我相信这纯粹是一个 .htaccess 问题。

我有几个典型的 URL 模式:

mysite.com/<page name here>, i.e. mysite.com/faq  
mysite.com/admin/<action here>, i.e. mysite.com/admin/login

.htaccess目前是:

RewriteEngine On

RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]

RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/page/id/$1 [L]

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

现在,应该很明显,mysite.com/faq应该重写为mysite.com/page/id/faq,但它正在抛出 404 。

只是要mysite.com成功加载默认控制器,在这种情况下是主页或等效于mysite.com/home. 所有管理员网址都运行良好。mysite.com/page/id/<page name here>也工作得很好。

只是为了比较,在.htaccess我升级 CodeIgniter 之前已经运行了 6 年的原始版本是:

RewriteRule ^((images|styles|scripts|documents)/*.*)$ $1 [L]
RewriteRule ^(admin/*)$ index.php/$1 [L]
RewriteRule ^([a-zA-Z0-9]+)$ page/id/$1
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php/$1 [L]

这个原始的行为与我目前在上面尝试使用的行为完全相同。原文的重写日志如下:

[rid#1db5120/initial] (3) [perdir C:/wamp/www/jb2/] strip per-dir prefix: C:/wamp/www/jb2/faq -> faq
[rid#1db5120/initial] (3) [perdir C:/wamp/www/jb2/] applying pattern '^(.*)$' to uri 'faq'
[rid#1db5120/initial] (4) [perdir C:/wamp/www/jb2/] RewriteCond: input='/jb2/faq' pattern='^system.*' => not-matched
[rid#1db5120/initial] (3) [perdir C:/wamp/www/jb2/] strip per-dir prefix: C:/wamp/www/jb2/faq -> faq
[rid#1db5120/initial] (3) [perdir C:/wamp/www/jb2/] applying pattern '^(.*)$' to uri 'faq'
[rid#1db5120/initial] (4) [perdir C:/wamp/www/jb2/] RewriteCond: input='/jb2/faq' pattern='^application.*' => not-matched
[rid#1db5120/initial] (3) [perdir C:/wamp/www/jb2/] strip per-dir prefix: C:/wamp/www/jb2/faq -> faq
[rid#1db5120/initial] (3) [perdir C:/wamp/www/jb2/] applying pattern '^((images|styles|scripts|documents)/*.*)$' to uri 'faq'
[rid#1db5120/initial] (3) [perdir C:/wamp/www/jb2/] strip per-dir prefix: C:/wamp/www/jb2/faq -> faq
[rid#1db5120/initial] (3) [perdir C:/wamp/www/jb2/] applying pattern '^(admin/*)$' to uri 'faq'
[rid#1db5120/initial] (3) [perdir C:/wamp/www/jb2/] strip per-dir prefix: C:/wamp/www/jb2/faq -> faq
[rid#1db5120/initial] (3) [perdir C:/wamp/www/jb2/] applying pattern '^([a-zA-Z0-9]+)$' to uri 'faq'
[rid#1db5120/initial] (2) [perdir C:/wamp/www/jb2/] rewrite 'faq' -> 'page/id/faq'
[rid#1db5120/initial] (3) [perdir C:/wamp/www/jb2/] add per-dir prefix: page/id/faq -> C:/wamp/www/jb2/page/id/faq
[rid#1db5120/initial] (3) [perdir C:/wamp/www/jb2/] strip per-dir prefix: C:/wamp/www/jb2/page/id/faq -> page/id/faq
[rid#1db5120/initial] (3) [perdir C:/wamp/www/jb2/] applying pattern '^(.*)$' to uri 'page/id/faq'
[rid#1db5120/initial] (4) [perdir C:/wamp/www/jb2/] RewriteCond: input='page/id/faq' pattern='!^(index\.php|robots\.txt|favicon\.ico)' => matched
[rid#1db5120/initial] (2) [perdir C:/wamp/www/jb2/] rewrite 'page/id/faq' -> 'index.php/page/id/faq'
[rid#1db5120/initial] (3) [perdir C:/wamp/www/jb2/] add per-dir prefix: index.php/page/id/faq -> C:/wamp/www/jb2/index.php/page/id/faq
[rid#1db5120/initial] (2) [perdir C:/wamp/www/jb2/] strip document_root prefix: C:/wamp/www/jb2/index.php/page/id/faq -> /jb2/index.php/page/id/faq
[rid#1db5120/initial] (1) [perdir C:/wamp/www/jb2/] internal redirect with /jb2/index.php/page/id/faq [INTERNAL REDIRECT]
[rid#4f657d8/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] add path info postfix: C:/wamp/www/jb2/index.php -> C:/wamp/www/jb2/index.php/page/id/faq
[rid#4f657d8/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] strip per-dir prefix: C:/wamp/www/jb2/index.php/page/id/faq -> index.php/page/id/faq
[rid#4f657d8/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] applying pattern '^(.*)$' to uri 'index.php/page/id/faq'
[rid#4f657d8/initial/redir#1] (4) [perdir C:/wamp/www/jb2/] RewriteCond: input='/jb2/index.php/page/id/faq' pattern='^system.*' => not-matched
[rid#4f657d8/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] add path info postfix: C:/wamp/www/jb2/index.php -> C:/wamp/www/jb2/index.php/page/id/faq
[rid#4f657d8/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] strip per-dir prefix: C:/wamp/www/jb2/index.php/page/id/faq -> index.php/page/id/faq
[rid#4f657d8/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] applying pattern '^(.*)$' to uri 'index.php/page/id/faq'
[rid#4f657d8/initial/redir#1] (4) [perdir C:/wamp/www/jb2/] RewriteCond: input='/jb2/index.php/page/id/faq' pattern='^application.*' => not-matched
[rid#4f657d8/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] add path info postfix: C:/wamp/www/jb2/index.php -> C:/wamp/www/jb2/index.php/page/id/faq
[rid#4f657d8/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] strip per-dir prefix: C:/wamp/www/jb2/index.php/page/id/faq -> index.php/page/id/faq
[rid#4f657d8/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] applying pattern '^((images|styles|scripts|documents)/*.*)$' to uri 'index.php/page/id/faq'
[rid#4f657d8/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] add path info postfix: C:/wamp/www/jb2/index.php -> C:/wamp/www/jb2/index.php/page/id/faq
[rid#4f657d8/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] strip per-dir prefix: C:/wamp/www/jb2/index.php/page/id/faq -> index.php/page/id/faq
[rid#4f657d8/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] applying pattern '^(admin/*)$' to uri 'index.php/page/id/faq'
[rid#4f657d8/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] add path info postfix: C:/wamp/www/jb2/index.php -> C:/wamp/www/jb2/index.php/page/id/faq
[rid#4f657d8/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] strip per-dir prefix: C:/wamp/www/jb2/index.php/page/id/faq -> index.php/page/id/faq
[rid#4f657d8/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] applying pattern '^([a-zA-Z0-9]+)$' to uri 'index.php/page/id/faq'
[rid#4f657d8/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] add path info postfix: C:/wamp/www/jb2/index.php -> C:/wamp/www/jb2/index.php/page/id/faq
[rid#4f657d8/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] strip per-dir prefix: C:/wamp/www/jb2/index.php/page/id/faq -> index.php/page/id/faq
[rid#4f657d8/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] applying pattern '^(.*)$' to uri 'index.php/page/id/faq'
[rid#4f657d8/initial/redir#1] (4) [perdir C:/wamp/www/jb2/] RewriteCond: input='index.php/page/id/faq' pattern='!^(index\.php|robots\.txt|favicon\.ico)' => not-matched
[rid#4f657d8/initial/redir#1] (1) [perdir C:/wamp/www/jb2/] pass through C:/wamp/www/jb2/index.php

.htaccess我一直在尝试的当前重写日志是这样的:

[rid#4fb2e70/initial] (3) [perdir C:/wamp/www/jb2/] strip per-dir prefix: C:/wamp/www/jb2/faq -> faq
[rid#4fb2e70/initial] (3) [perdir C:/wamp/www/jb2/] applying pattern '^(.*)$' to uri 'faq'
[rid#4fb2e70/initial] (4) [perdir C:/wamp/www/jb2/] RewriteCond: input='/jb2/faq' pattern='^system.*' => not-matched
[rid#4fb2e70/initial] (3) [perdir C:/wamp/www/jb2/] strip per-dir prefix: C:/wamp/www/jb2/faq -> faq
[rid#4fb2e70/initial] (3) [perdir C:/wamp/www/jb2/] applying pattern '^(.*)$' to uri 'faq'
[rid#4fb2e70/initial] (4) [perdir C:/wamp/www/jb2/] RewriteCond: input='/jb2/faq' pattern='^application.*' => not-matched
[rid#4fb2e70/initial] (3) [perdir C:/wamp/www/jb2/] strip per-dir prefix: C:/wamp/www/jb2/faq -> faq
[rid#4fb2e70/initial] (3) [perdir C:/wamp/www/jb2/] applying pattern '^(.*)$' to uri 'faq'
[rid#4fb2e70/initial] (4) [perdir C:/wamp/www/jb2/] RewriteCond: input='C:/wamp/www/jb2/faq' pattern='!-f' => matched
[rid#4fb2e70/initial] (4) [perdir C:/wamp/www/jb2/] RewriteCond: input='C:/wamp/www/jb2/faq' pattern='!-d' => matched
[rid#4fb2e70/initial] (2) [perdir C:/wamp/www/jb2/] rewrite 'faq' -> 'index.php/page/id/faq'
[rid#4fb2e70/initial] (3) [perdir C:/wamp/www/jb2/] add per-dir prefix: index.php/page/id/faq -> C:/wamp/www/jb2/index.php/page/id/faq
[rid#4fb2e70/initial] (2) [perdir C:/wamp/www/jb2/] strip document_root prefix: C:/wamp/www/jb2/index.php/page/id/faq -> /jb2/index.php/page/id/faq
[rid#4fb2e70/initial] (1) [perdir C:/wamp/www/jb2/] internal redirect with /jb2/index.php/page/id/faq [INTERNAL REDIRECT]
[rid#1da50a0/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] add path info postfix: C:/wamp/www/jb2/index.php -> C:/wamp/www/jb2/index.php/page/id/faq
[rid#1da50a0/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] strip per-dir prefix: C:/wamp/www/jb2/index.php/page/id/faq -> index.php/page/id/faq
[rid#1da50a0/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] applying pattern '^(.*)$' to uri 'index.php/page/id/faq'
[rid#1da50a0/initial/redir#1] (4) [perdir C:/wamp/www/jb2/] RewriteCond: input='/jb2/index.php/page/id/faq' pattern='^system.*' => not-matched
[rid#1da50a0/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] add path info postfix: C:/wamp/www/jb2/index.php -> C:/wamp/www/jb2/index.php/page/id/faq
[rid#1da50a0/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] strip per-dir prefix: C:/wamp/www/jb2/index.php/page/id/faq -> index.php/page/id/faq
[rid#1da50a0/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] applying pattern '^(.*)$' to uri 'index.php/page/id/faq'
[rid#1da50a0/initial/redir#1] (4) [perdir C:/wamp/www/jb2/] RewriteCond: input='/jb2/index.php/page/id/faq' pattern='^application.*' => not-matched
[rid#1da50a0/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] add path info postfix: C:/wamp/www/jb2/index.php -> C:/wamp/www/jb2/index.php/page/id/faq
[rid#1da50a0/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] strip per-dir prefix: C:/wamp/www/jb2/index.php/page/id/faq -> index.php/page/id/faq
[rid#1da50a0/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] applying pattern '^(.*)$' to uri 'index.php/page/id/faq'
[rid#1da50a0/initial/redir#1] (4) [perdir C:/wamp/www/jb2/] RewriteCond: input='C:/wamp/www/jb2/index.php' pattern='!-f' => not-matched
[rid#1da50a0/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] add path info postfix: C:/wamp/www/jb2/index.php -> C:/wamp/www/jb2/index.php/page/id/faq
[rid#1da50a0/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] strip per-dir prefix: C:/wamp/www/jb2/index.php/page/id/faq -> index.php/page/id/faq
[rid#1da50a0/initial/redir#1] (3) [perdir C:/wamp/www/jb2/] applying pattern '^(admin/*)$' to uri 'index.php/page/id/faq'
[rid#1da50a0/initial/redir#1] (1) [perdir C:/wamp/www/jb2/] pass through C:/wamp/www/jb2/index.php

实际的升级过程只是在 CodeIgniter 框架上,并将文件重新组织到单独的applicationsystem文件夹中,除了升级过程中所需的最小更改之外,我没有触及任何实际的自定义代码。

我已经尝试了大约十亿个不同的.htaccess命令组合,试图让这个东西工作,但无法让“正常”页面和管理页面都使用相同的配置正确加载。最终我的问题是,为什么升级 CodeIgniter 会神奇地破坏 URL 重写,但我很乐意让它再次工作。

有人有建议吗?我希望对此有另一种看法。

4

2 回答 2

1

所以我终于得到了另一个论坛的建议。我$config[‘uri_protocol’]AUTOto改变了PATH_INFO,它神奇地起作用了。不过,我确实必须添加一行来处理admin/*URL 格式,所以我的最终结果.htaccess是:

RewriteRule ^(admin/*)$ index.php/$1 [L]
RewriteRule ^([a-zA-Z0-9]+)$ page/id/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]  
于 2012-08-17T21:20:36.003 回答
0

您的 .htaccess 文件是否在正确的文件夹中?CodeIgniter 使用了几个 .htaccess 文件,我相信系统和应用程序内部的那个说拒绝所有文件,而上面的那个有你的规则。

于 2012-08-17T20:09:38.393 回答