Apache mod_rewrite 通常可以工作,但在此文件夹中不起作用 - 出了什么问题?
我有一个 Apache Web 服务器,通常可以很好地处理 mod_rewrite。我有一个名为 /communications/q/
的目录,我想重写任何 URI 以在输入的 URI 的其余部分之前插入“index.php”。
例如,/communications/q/something/else
实际上应该提供 communications/q/index.php/something/else
。这是标准的 PHP CodeIgniter 设置。
我在 /q/ 目录中放置了一个 .htaccess
文件,并在其中添加了以下内容:
RewriteEngine On
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
当我尝试访问 /communications/q/ 时,我收到 404 Not Found 错误。这完全没有意义,因为如果我注释掉 .htaccess 的内容,如果我转到 /communications/q/,我就会得到 index.php 页面,但是使用代码,我会得到 404 Not Found。
有人发现我做错了什么吗?
仅供参考,我有一个名为 hello 的控制器,因此从技术上讲 /communications/q/hello 应该可以工作,但它也是一个 404。但随着 .htaccess 被注释掉, /communications/q/index.php/hello 工作正常。
..
==== 添加注释 #1 ====
使用 CodeIgniter,我应该能够使用 URI 结构调用控制器和函数。所以我实际上有一个名为 welcome
的控制器,然后是一个名为 index()
的默认函数,以及一个名为 hello()
的函数>。
CI 的工作方式是,我会编写 /communications/q/index.php/welcome
,然后从 welcome 中获取
index()
函数的输出控制器。事实上,现在效果很好。
不幸的是,在 URI 中包含奇怪的 index.php
既不方便也没有必要,因此 CI 建议使用 .htaccess 来允许 URI 省略 URI 的该部分,并使用 mod_rewrite 在后台默默地重新输入它。
然而,当我添加上面的 RewriteRule 时,它不起作用。因此:
当 /controller/q/welcome
应该返回与 /controller/q/index.php/welcome
完全相同的内容时,它会返回 404 错误。这就是问题所在。上面的 RewriteRule 不应该使它起作用吗?
..
I have an Apache web server that usually handles mod_rewrite fine. I have a directory called /communications/q/
and I want to rewrite any URI to insert "index.php" before the rest of the entered URI.
For example, /communications/q/something/else
should actually serve communications/q/index.php/something/else
. It's the standard PHP CodeIgniter setup.
I placed a .htaccess
file in the /q/ directory and put the following in it:
RewriteEngine On
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
When I even try to go to /communications/q/, I get a 404 Not Found error. Which makes no sense at all because if I comment the .htaccess stuff out, I get the index.php page if I go to /communications/q/, but with the code, I get 404 Not Found.
Anyone spot what I'm doing wrong?
FYI I have a controller called hello, so technically /communications/q/hello should work, but it's a 404, also. But with .htaccess commented out, /communications/q/index.php/hello works fine.
..
==== ADDED NOTE #1 ====
Using CodeIgniter, I should be able to call controllers and functions using the URI structure. So I have a controller called welcome
, actually, and then a function called index()
which is the default, and a function called hello()
.
The way CI works, I would write /communications/q/index.php/welcome
and I would get the output of the index()
function from the welcome
controller. And in fact, this works perfectly right now.
Unfortunately, having that weird index.php
in the URI is unwieldy and unnecessary, so CI suggests using .htaccess to allow the URI to omit that section of the URI and silently reenter it in the background, using mod_rewrite.
When I add the RewriteRule above, however, it doesn't work. So:
/controller/q/welcome
returns a 404 error when it should return exactly the same thing as /controller/q/index.php/welcome
. That's the problem. Shouldn't the RewriteRule above make that work?
..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
RewriteRule
中的替换是相对于DocumentRoot
的。基于此,我建议您尝试:The substitution in a
RewriteRule
is relative to theDocumentRoot
. Based on this, I'd suggest you try:最后,答案就在 CodeIgniter wiki 中。我用以下代码替换了 .htaccess 代码:
一切都运行良好。我认为主要的相关更改是在
index.php
之后向 RewriteRule 添加?
——有人明白为什么这是必要的吗?来源:http://codeigniter.com/wiki/mod_rewrite/
The answer was in the CodeIgniter wiki, in the end. I replaced my .htaccess code with the following:
And everything worked perfectly. The major relevant change, I think, was adding a
?
to the RewriteRule afterindex.php
-- does anyone understand why that's necessary?Source: http://codeigniter.com/wiki/mod_rewrite/