.htaccess 破坏了所有 css 和 js,尽管我设置了绝对路径
这是我的 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您的问题,您的 .htaccess 文件似乎位于 /finance/ 文件夹中。如果是这种情况,问题在于您的第二个
RewriteRule
将与您的脚本和sylesheet 的请求相匹配。您已有的条件可以让您避免此问题,但它们仅适用于它们之后的第一条规则(您的搜索规则)。与往常一样,有几种不同的方法可以解决问题,但最简单的方法是复制
RewriteCond
块,以便它也适用于第二条规则。这看起来如下所示:请注意,您甚至可能不需要第一条规则的条件块,因为您的任何实际文件可能都不太可能与该测试模式匹配。但第二个肯定需要它,以防止您的资源被误导。
替代方法
在此方法中,如果您的任何条件匹配,我们将跳过接下来的两条规则(由
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: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
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 theL
flag in place of theS=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.RewriteCond
指令仅属于下一个RewriteRule
。因此,您的三个RewriteCond
指令仅属于第一个RewriteRule
,而不属于没有条件的第二个。因此,要排除第二个
RewriteRule
的现有文件和目录,您也需要在其他规则前面编写相同的RewriteCond
:但您也可以使用规则当请求可以映射到现有文件或目录时停止重写过程:
将此规则放在其他规则之前,重写过程将以此规则停止。
A
RewriteCond
directive does only belong to the next followingRewriteRule
. So your threeRewriteCond
directives do only belong to the firstRewriteRule
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 sameRewriteCond
s in front of your other rule too:But you can also use a rule that stops the rewriting process when the request can be mapped onto an existing file or directory:
Put this rule in front of your other rules and the rewriting process will stop with this rule.