htaccess mod_rewrite 重定向到 CodeIgniter 控制器而不是子文件夹

发布于 2024-12-27 11:31:17 字数 899 浏览 2 评论 0 原文

我正在使用 CodeIgniter,并在 .htaccess 中添加了一些规则,将所有对 index.php/a/b 的请求重定向到 /a/b 这些是规则:

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

我想在 apache 中禁用目录列表,所以我添加了以下规则:

Options -Indexes

问题是,一旦我添加此规则,它会禁用目录列表,但也会将任何调用重定向到特定文件夹(不包含索引.* 文件)到 CodeIgniter 控制器(该控制器不存在,并生成 404 错误)。

之前:访问 /X 文件夹将返回目录列表

之后:访问 /X 文件夹将静默调用 index.php/X 并返回 404 错误。

我希望对 /X 及其中的任何子文件夹的任何访问都只会返回 403 禁止错误(或任何不涉及运行 php 脚本的错误),而不是调用 index.php/X (我希望用户能够访问 /X 及其子文件夹中的文件,但不允许用户查看 /X 及其子文件夹的目录列表)。

我尝试过,但没有成功:

  • 添加 规则。看起来我无法在 .htaccess 中使用它,
  • RewriteRule ^(.*)$ index.php 之前添加 RewriteCond $1 !^(X.*|/X.*) 规则/$1 [L](见上文)。这似乎不起作用。

我应该在 .htaccess 中写什么来解决这个问题?

I'm using CodeIgniter and I added some rules in .htaccess to redirect all requests to index.php/a/b to /a/b
These are the rules:

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

I want to disable directory listing in apache, so I added the following rule:

Options -Indexes

The problem is that once I added this rule, it disables directory listing but also redirects any call to a specific folder (that doesn't contains an index.* file) to a CodeIgniter Controller (which doesn't exists, and generates a 404 error).

Before: access to a /X folder would return a directory listing

After: access to a /X folder would call index.php/X silently and return a 404 error.

I want that any access to /X and any subfolder in it simply return a 403 forbidden error (or any error that doesn't involve running a php script), not call index.php/X (I want to users to be able to access files in /X and its subfolders, but not allow users to see a directory listing of /X and its subfolders).

I have tried, without success:

  • adding a <Directory "/X"> rule. Looks like i can't use it in my .htaccess
  • adding a RewriteCond $1 !^(X.*|/X.*) rule before RewriteRule ^(.*)$ index.php/$1 [L] (see above). It doesn't seem to work.

What should I write in .htaccess to solve this problem ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

风渺 2025-01-03 11:31:17

要排除以 /X 开头的任何文件夹,请使用下面添加的#1 ReWriteCond

要为 /X 和子目录返回 403,请取消注释 #1 之后的两行,并注释 #2 之后的行。顺序很重要,所以如果你想让它发挥作用,这条规则必须放在第一位

RewriteEngine On
RewriteBase /

#1 to return a 403, for /X, uncomment these 2 lines
#RewriteCond %{REQUEST_URI} ^/X [NC]
#RewriteRule . - [L,F]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#2 exclude any folder starting with X
RewriteCond %{THE_REQUEST} !^[A-Z]{3,9}\ /X [NC]
RewriteRule ^(.*)$ index.php/$1 [L]

To exclude any folder starting with /X use the added #1 ReWriteCond below.

To return a 403 for /X and sub-directories, uncomment the two lines following #1, and comment the line following #2. Order is important, so this rule must come first if you want it to work

RewriteEngine On
RewriteBase /

#1 to return a 403, for /X, uncomment these 2 lines
#RewriteCond %{REQUEST_URI} ^/X [NC]
#RewriteRule . - [L,F]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#2 exclude any folder starting with X
RewriteCond %{THE_REQUEST} !^[A-Z]{3,9}\ /X [NC]
RewriteRule ^(.*)$ index.php/$1 [L]
睫毛溺水了 2025-01-03 11:31:17

您不能在 .htaccess 中使用 ,因为 .htaccess 是一个 指令,导出到 apache 编译配置之外,并在每个请求的运行时间。是的 .htaccess 很糟糕并且执行速度非常慢,但这也意味着您可以删除所有 .htaccess 并将其替换为 apache 配置文件中正确的 内容(如果您有权访问此类)文件。

现在你的问题了。您想要检测对目录 /X 的直接访问,并为此发送一个 http 错误代码。要将此规则扩展到所有不包含 index.php 文件的目录 /foo/foo/bar 我只需编写:

# detect requested url IS a directory
RewriteCond %{REQUEST_FILENAME} -d
# detect the requested directory does not contain an index.php file
RewriteCond %{REQUEST_FILENAME}index.php !-f
# then bail out
RewriteRule ^(.*)$ - [R=403,L]

You cannot use <Directory /X> in a .htaccess because a .htaccess is a <Directory> instruction, exported outside of the apache compiled configuration, and checked at runtime for every request. yes .htaccess is bad and performs very slow, but that also mean you could remove all your .htaccess and replace it with the right <Directory> things in the apache configuration files if you have access to such files.

Now your problem. You want to detect directaccess to a directory, /X, and send an http error code for that. To extend this rule to all directories /foo or /foo/bar which do not contains index.php files I would simply write:

# detect requested url IS a directory
RewriteCond %{REQUEST_FILENAME} -d
# detect the requested directory does not contain an index.php file
RewriteCond %{REQUEST_FILENAME}index.php !-f
# then bail out
RewriteRule ^(.*)$ - [R=403,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文