Htaccess 帮助,$1 在设置之前被使用???什么?

发布于 2024-08-24 07:46:20 字数 352 浏览 3 评论 0原文

我有这个片段

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond $1 !^(index\.php|images|img|css|js|robots\.txt)
RewriteRule (.*) index.php?/$1 [L] 

它不允许我访问 website.com/js/main.php 上的文件 但它会让我访问index.php

根据我的网络主机,$1 在设置之前被调用。有什么解决办法吗?

明天回来时我会接受答案。谢谢你!

I have this snippet

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond $1 !^(index\.php|images|img|css|js|robots\.txt)
RewriteRule (.*) index.php?/$1 [L] 

It won't allow me to access a file at website.com/js/main.php
but it will let me access index.php

According to my webhost, $1 is being called before it is set. Any solutions?

I'll accept answers when i get back tomorrow. Thank you!

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

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

发布评论

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

评论(2

¢蛋碎的人ぎ生 2024-08-31 07:46:20

我假设您希望重写忽略您的条件当前指定的内容。在这种情况下...

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond !^(index\.php|images|img|css|js|robots\.txt)
RewriteRule (.*) index.php?/$1 [L,QSA]

...应该可以正常工作。您可能希望那里有 QSA,以便在有查询字符串时能够得到正确处理。

I'm assuming you want the rewrite to ignore the things which your condition currently specifies. In that case...

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond !^(index\.php|images|img|css|js|robots\.txt)
RewriteRule (.*) index.php?/$1 [L,QSA]

...should work fine. You'll probably want the QSA on there so that if there's a query string, it's properly handled.

咽泪装欢 2024-08-31 07:46:20

您的虚拟主机错误。 规则集处理顺序是:

  1. 中的模式测试 RewriteRule(将用值填充 $N 引用)
  2. 进行测试

关联的 RewriteCond 条件(如果存在)仅当模式与当前 URL 路径匹配时 并且满足关联条件时,将应用该模式。

因此,在您的情况下,模式 (.*) 在当前 URL 路径 js/main.php 上进行测试(没有本地前缀 /)。它匹配 ($0=js/main.php, $1=js/main.php),所以这三个关联条件按照出现的顺序进行测试:

  1. RewriteCond %{REQUEST_FILENAME} !-f
  2. RewriteCond %{REQUEST_FILENAME} !-d
  3. RewriteCond $1 !^(index\ .php|images|img|css|js|robots\.txt)

假设请求的 URL 路径 /js/main.php 不是引用现有的文件或目录,则第一个条件都为真。但第三个将评估为 false,如 $1=js/main.php 和模式 ^(index\.php|images|img|css|js |robots\.txt) 匹配 (^js 分支) js/main.php。因此,条件未满足,规则未应用。

Your web host is wrong. The order of ruleset processing is:

  1. pattern in RewriteRule is tested (that will populate the $N references with values)
  2. associated RewriteCond conditions are tested (if present)

Only if the pattern matches the current URL path and the associated condition is fulfilled, the pattern is applied.

So in your case the pattern (.*) is tested on the current URL path js/main.php (without local prefix /). It matches ($0=js/main.php, $1=js/main.php) so the three associated conditions are tested in the order they appear:

  1. RewriteCond %{REQUEST_FILENAME} !-f
  2. RewriteCond %{REQUEST_FILENAME} !-d
  3. RewriteCond $1 !^(index\.php|images|img|css|js|robots\.txt)

Assuming that the requested URL path /js/main.php does not refer to an existing file or directory, the first conditions are both true. But the third one will evaluate to false as $1=js/main.php and the pattern ^(index\.php|images|img|css|js|robots\.txt) matches (^js branch) js/main.php. So the condition is not fulfilled and the rule is not applied.

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