如何从 mod_rewrite 规则中排除 CSS、JS、JPG、GIF 文件?

发布于 2024-09-18 13:03:36 字数 542 浏览 11 评论 0原文

这是我到目前为止所拥有的:

RewriteRule ^([A-Za-z0-9_]*)/?$ index.php?page=$1 [NC]
RewriteRule ^([A-Za-z0-9_]*)/([A-Za-z0-9_]*)/?$ index.php?page=$1/$2 [NC]
RewriteRule ^([A-Za-z0-9_]*)/([A-Za-z0-9_]*)/([A-Za-z0-9_]*)/?$ index.php?page=$1/$2/$3 [NC]

我的所有 css 文件都位于 /ssi/ 中。

站点结构本身是 /index.php?page=$whatever

$whatever 有时包含 /'s,所以如果我转到 /whatever/whatever2,按照我当前的规则,它假设 css 位于 /whatever/ssi/* .css。

我希望这一切都是有道理的。

所以,基本上我只是希望能够编写一个条件,表示“如果它是 css 文件,则不要应用这些规则”。

感谢您对我能得到的任何帮助:)。

This is what I have so far:

RewriteRule ^([A-Za-z0-9_]*)/?$ index.php?page=$1 [NC]
RewriteRule ^([A-Za-z0-9_]*)/([A-Za-z0-9_]*)/?$ index.php?page=$1/$2 [NC]
RewriteRule ^([A-Za-z0-9_]*)/([A-Za-z0-9_]*)/([A-Za-z0-9_]*)/?$ index.php?page=$1/$2/$3 [NC]

All of my css files are located in /ssi/.

The site structure itself is /index.php?page=$whatever

$whatever sometimes includes /'s, so if I go to /whatever/whatever2, with my current rules, it assumes the css is located in /whatever/ssi/*.css.

I hope all of that makes sense.

So, basically I just want to be able to write a condition that says "if it's a css file, don't apply these rules."

Thank you for any help I can get :).

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

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

发布评论

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

评论(4

-残月青衣踏尘吟 2024-09-25 13:03:36

您遇到的问题不是 mod_rewrite 应用于 css 文件,而是 html 中到 css 的路径是相对的。浏览器只是从正确的相对路径请求 css。你确实有两个选择。

  1. 使 css 的路径相对于域而不是页面(例如使用 /ssi/styles.css 而不是 ssi/styles.css)
  2. 创建重写规则以将所有 css 重定向到正确的 URL。 (例如 RewriteRule (*.css) /ssi/$1

The problem that you are experiencing is not that the mod_rewrite is being applied to css files, but rather that the paths in your html to your css is relative. The browser is simply requesting the css from the correct relative path. You really have two options.

  1. Make your paths to your css relative to the domain instead of to the page (e.g. use /ssi/styles.css instead of ssi/styles.css)
  2. Create a rewrite rule to redirect all css to the correct URL. (e.g. RewriteRule (*.css) /ssi/$1
合久必婚 2024-09-25 13:03:36

预计您将使用带有 mod_rewrite 模块的 Apache,您应该尝试 RewriteCond 指令。以下示例使用 RewriteRule 指令排除 RewriteCond 中以下行的所有匹配项。
规则基本上是正则表达式。我的示例中的排除了以 favicon.ico 和 css 开头的所有内容,css 是我的样式表所在的文件夹。

RewriteEngine On
RewriteCond $1 !^(favicon\.ico|favicon\.png|media|robots\.txt|crossdomain\.xml|css|js)
RewriteRule ^(.*)$ index.php/$1 [L]

进一步阅读:
http://httpd.apache.org/docs/2.0/mod/ mod_rewrite.html#rewritecond

Anticipating you're using Apache with mod_rewrite module, you should try the RewriteCond Directive. The following example excludes all the matches from RewriteCond for the following line with the RewriteRule Directive.
The rules are basically regular expression. The one in my example excludes everything which starts with either favicon.ico as well as with css, which is the folder, where my Stylesheets resides.

RewriteEngine On
RewriteCond $1 !^(favicon\.ico|favicon\.png|media|robots\.txt|crossdomain\.xml|css|js)
RewriteRule ^(.*)$ index.php/$1 [L]

Further reading:
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritecond

梦在深巷 2024-09-25 13:03:36

...它假设 css 位于 /whatever/ssi/*.css 中。
我只是希望能够编写一个条件,表示“如果它是 css 文件,则不要应用这些规则。”

这直接翻译成

RewriteCond %{REQUEST_URI} !^/whatever/ssi/[^/]+\.css$
RewriteRule ...

... it assumes the css is located in /whatever/ssi/*.css.
I just want to be able to write a condition that says "if it's a css file, don't apply these rules."

This translates directly to

RewriteCond %{REQUEST_URI} !^/whatever/ssi/[^/]+\.css$
RewriteRule ...
浅听莫相离 2024-09-25 13:03:36

我想我的解决方案将是最实证的解决方案,但它有效:

RewriteEngine On

RewriteBase /

RewriteRule ^([a-z0-9-]+)$ /?page=$1 [NC,L]

RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)$ /?page=$1&action=$2 [NC,L]

这将翻译 http://example .com/?page=contact&action=sendhttp://example.com/contact/send
我的页面值只是字母数字,并包含减号作为单词分隔符,但您可以替换这些值以匹配您需要的任何值。

当然,您需要在页面中为所有 js、css、img 目录添加斜杠作为前缀。所以 css/style.css 将是 /css/style.css 或使用绝对 url。

I guess my solution will be the most empirical one but it worked:

RewriteEngine On

RewriteBase /

RewriteRule ^([a-z0-9-]+)$ /?page=$1 [NC,L]

RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)$ /?page=$1&action=$2 [NC,L]

That will translate http://example.com/?page=contact&action=send to http://example.com/contact/send
My page values were only alphanumeric and contain minus sign as word separator but you can replace the values to match whatever you need.

And, of course you need to prefix all of your js, css, img directories with a slash in the pages. So css/style.css would be /css/style.css or use absolute url.

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