Apache mod_rewrite 通常可以工作,但在此文件夹中不起作用 - 出了什么问题?

发布于 2024-09-27 10:22:40 字数 1504 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟柳画桥 2024-10-04 10:22:40

RewriteRule 中的替换是相对于 DocumentRoot 的。基于此,我建议您尝试:

RewriteEngine On
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /communications/q/index.php/$1 [L]

The substitution in a RewriteRule is relative to the DocumentRoot. Based on this, I'd suggest you try:

RewriteEngine On
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /communications/q/index.php/$1 [L]
有深☉意 2024-10-04 10:22:40

最后,答案就在 CodeIgniter wiki 中。我用以下代码替换了 .htaccess 代码:

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /communications/q/

# Removes access to the system folder by users.
# Additionally this will allow you to create a System.php controller,
# previously this would not have been possible.
# 'system' can be replaced if you have renamed your system folder.

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

# When your application folder isn't in the system folder
# This snippet prevents user access to the application folder
# Submitted by: Fabdrol
# Rename 'application' to your applications folder name.

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

# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php

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

</IfModule>

<IfModule !mod_rewrite.c>

# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin

ErrorDocument 404 /index.php

</IfModule>

一切都运行良好。我认为主要的相关更改是在 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:

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /communications/q/

# Removes access to the system folder by users.
# Additionally this will allow you to create a System.php controller,
# previously this would not have been possible.
# 'system' can be replaced if you have renamed your system folder.

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

# When your application folder isn't in the system folder
# This snippet prevents user access to the application folder
# Submitted by: Fabdrol
# Rename 'application' to your applications folder name.

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

# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php

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

</IfModule>

<IfModule !mod_rewrite.c>

# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin

ErrorDocument 404 /index.php

</IfModule>

And everything worked perfectly. The major relevant change, I think, was adding a ? to the RewriteRule after index.php -- does anyone understand why that's necessary?

Source: http://codeigniter.com/wiki/mod_rewrite/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文