.htaccess 破坏了所有 css 和 js,尽管我设置了绝对路径

发布于 2024-09-26 18:00:49 字数 679 浏览 1 评论 0原文

这是我的 .htaccess 文件,

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !=cms

RewriteRule ^([^/]*)/search/([^/]+)$  index.php?lang=$1&id=search&searchword=$2 [L]
RewriteRule ^([^/]*)/([^/]*)$  index.php?lang=$1&id=$2 [L]

它没有按我的预期工作。

我的 .css,.js 文件具有绝对路径,例如 http://bs.am/finance/css/style.css (我的finance 文件夹中的所有文件)

第一条规则破坏了所有 css 和 js 文件,但如果我替换 rewriteRules 的位置,则工作正常。

有人可以解释这种行为吗?

我还尝试了 RewriteCond %{REQUEST_FILENAME} !^(.+)\.css$ 但没有帮助。

非常感谢

Here is my .htaccess file

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !=cms

RewriteRule ^([^/]*)/search/([^/]+)$  index.php?lang=$1&id=search&searchword=$2 [L]
RewriteRule ^([^/]*)/([^/]*)$  index.php?lang=$1&id=$2 [L]

It doesn't work as i expect.

My .css,.js files has absolute path, like http://bs.am/finance/css/style.css (my all files in finance folder)

The first rule breaks all css and js files, but if i replace the places of rewriteRules, works fine.

Could somebody explain such behavior?

I also tried RewriteCond %{REQUEST_FILENAME} !^(.+)\.css$ but doesn't help.

Thanks much

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

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

发布评论

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

评论(2

莳間冲淡了誓言ζ 2024-10-03 18:00:49

根据您的问题,您的 .htaccess 文件似乎位于 /finance/ 文件夹中。如果是这种情况,问题在于您的第二个 RewriteRule 将与您的脚本和sylesheet 的请求相匹配。

您已有的条件可以让您避免此问题,但它们仅适用于它们之后的第一条规则(您的搜索规则)。与往常一样,有几种不同的方法可以解决问题,但最简单的方法是复制 RewriteCond 块,以便它也适用于第二条规则。这看起来如下所示:

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 =cms
RewriteRule ^([^/]*)/search/([^/]+)$  index.php?lang=$1&id=search&searchword=$2 [L]

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

请注意,您甚至可能不需要第一条规则的条件块,因为您的任何实际文件可能都不太可能与该测试模式匹配。但第二个肯定需要它,以防止您的资源被误导。

替代方法

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond $1 !=cms
RewriteRule ^([^/]*) - [S=2]

RewriteRule ^([^/]*)/search/([^/]+)$  index.php?lang=$1&id=search&searchword=$2 [L]
RewriteRule ^([^/]*)/([^/]*)$  index.php?lang=$1&id=$2 [L]

在此方法中,如果您的任何条件匹配,我们将跳过接下来的两条规则(由 S=2 表示)。这与应用条件块相同对于他们两个来说。 Gumbo 提到,您还可以使用 L 标志代替 S=2 来忽略该块之后的每个规则(在您的条件下)这些选项中哪一个最合适取决于您将来可能想要添加的其他规则。

Based on your question, it seems that your .htaccess file is in the /finance/ folder. If this is the case, the problem lies in the fact that your second RewriteRule will match the requests for your scripts and sylesheets.

The conditions you have already will allow you to avoid this problem, but they only apply to the first rule that comes after them (your search rule). As usual, there are a few different ways to fix things, but the simplest is to copy your RewriteCond block so that it applies to the second rule too. This would look like the following:

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 =cms
RewriteRule ^([^/]*)/search/([^/]+)$  index.php?lang=$1&id=search&searchword=$2 [L]

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

Note that you might not even need the condition block for the first rule, as it's probably unlikely that any of your real files match that test pattern. It is definitely needed on the second one though, to prevent your resources from being misdirected.

Alternative Approach

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond $1 !=cms
RewriteRule ^([^/]*) - [S=2]

RewriteRule ^([^/]*)/search/([^/]+)$  index.php?lang=$1&id=search&searchword=$2 [L]
RewriteRule ^([^/]*)/([^/]*)$  index.php?lang=$1&id=$2 [L]

In this approach, if any of your conditions match, we skip the next two rules (indicated by the S=2. This is the same as applying the condition block to both of them. As Gumbo mentions, you could also use the L flag in place of the S=2 to ignore every rule that comes after that block in the case of your conditions matching. Which of these options is most appropriate depends on what other rules you might want to add in the future.

安人多梦 2024-10-03 18:00:49

RewriteCond 指令仅属于下一个 RewriteRule。因此,您的三个 RewriteCond 指令仅属于第一个 RewriteRule,而不属于没有条件的第二个。

因此,要排除第二个 RewriteRule 的现有文件和目录,您也需要在其他规则前面编写相同的 RewriteCond

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !=cms
RewriteRule ^([^/]*)/search/([^/]+)$  index.php?lang=$1&id=search&searchword=$2 [L]

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

但您也可以使用规则当请求可以映射到现有文件或目录时停止重写过程:

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

将此规则放在其他规则之前,重写过程将以此规则停止。

A RewriteCond directive does only belong to the next following RewriteRule. So your three RewriteCond directives do only belong to the first RewriteRule but not to the second that has no conditions.

So to exclude existing files and directories for the second RewriteRule, you would need to write the same RewriteConds in front of your other rule too:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !=cms
RewriteRule ^([^/]*)/search/([^/]+)$  index.php?lang=$1&id=search&searchword=$2 [L]

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

But you can also use a rule that stops the rewriting process when the request can be mapped onto an existing file or directory:

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

Put this rule in front of your other rules and the rewriting process will stop with this rule.

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