htaccess - 静态文件的 RewriteRule

发布于 2024-10-08 04:54:41 字数 677 浏览 0 评论 0原文

如何将图像和其他静态文件的网址重写到特定文件夹,即我到目前为止正在使用这个:

RewriteRule \.(woff|ttf|svg|js|ico|gif|jpg|png|css|htc|xml|txt)$ /MyFolder/$1

但它不起作用,

请帮忙。

------------------- 更新 ----------------------

好的,现在我有了:

RewriteCond %{HTTP_HOST} ^(www.)?exmpale.com$
RewriteCond %{REQUEST_URI} !^/exmpale.com/
RewriteRule ^(.*\.(woff|ttf|svg|js|ico|gif|jpg|png|css|htc|xml|txt))$ /exmpale.com/$1

RewriteCond %{HTTP_HOST} ^(www.)?exmpale.com$
RewriteRule !\.(woff|ttf|svg|js|ico|gif|jpg|png|css|htc|xml|txt)$ /exmpale.com/index.php

所以图像重定向exmpale 文件夹工作,但现在我想将其他所有内容重写到 index.php,上述解决方案到目前为止不起作用。

干杯, /马尔辛

how to rewrite urls for images and other static files to specific folder, i.e. I am using this so far:

RewriteRule \.(woff|ttf|svg|js|ico|gif|jpg|png|css|htc|xml|txt)$ /MyFolder/$1

but its not working,

please help.

------------------- update ---------------------

ok so now I have:

RewriteCond %{HTTP_HOST} ^(www.)?exmpale.com$
RewriteCond %{REQUEST_URI} !^/exmpale.com/
RewriteRule ^(.*\.(woff|ttf|svg|js|ico|gif|jpg|png|css|htc|xml|txt))$ /exmpale.com/$1

RewriteCond %{HTTP_HOST} ^(www.)?exmpale.com$
RewriteRule !\.(woff|ttf|svg|js|ico|gif|jpg|png|css|htc|xml|txt)$ /exmpale.com/index.php

so images redirected to exmpale folders work but now I would like to rewrite everything else to index.php, above solution is not working so far.

cheers,
/Marcin

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

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

发布评论

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

评论(2

夜访吸血鬼 2024-10-15 04:54:41

也许这就是您想要的:

RewriteRule ^(.*\.(woff|ttf|svg|js|ico|gif|jpg|png|css|htc|xml|txt))$ /MyFolder/$1

或者也许是这样:

RewriteRule ([^/]*\.(woff|ttf|svg|js|ico|gif|jpg|png|css|htc|xml|txt))$ /MyFolder/$1

如果这些都不是您想要的,请添加详细信息以及您所需的输入/输出的一些示例。

至于您的更新,您可能希望使用 L 标志在该规则匹配时停止规则处理。例如:

RewriteCond %{HTTP_HOST} ^(www.)?exmpale.com$
RewriteCond %{REQUEST_URI} !^/exmpale.com/

# The next line is long. Scroll to the end!
RewriteRule ^(.*\.(woff|ttf|svg|js|ico|gif|jpg|png|css|htc|xml|txt))$ /exmpale.com/$1 [L]

RewriteCond %{HTTP_HOST} ^(www.)?exmpale.com$
RewriteRule .* /exmpale.com/index.php

Maybe this is what you want:

RewriteRule ^(.*\.(woff|ttf|svg|js|ico|gif|jpg|png|css|htc|xml|txt))$ /MyFolder/$1

or perhaps this:

RewriteRule ([^/]*\.(woff|ttf|svg|js|ico|gif|jpg|png|css|htc|xml|txt))$ /MyFolder/$1

If neither of those is what you're looking for please add details with some examples of your desired inputs/outputs.

As for your update, you probably want to use the L flag to stop rule processing when this rule matches. eg:

RewriteCond %{HTTP_HOST} ^(www.)?exmpale.com$
RewriteCond %{REQUEST_URI} !^/exmpale.com/

# The next line is long. Scroll to the end!
RewriteRule ^(.*\.(woff|ttf|svg|js|ico|gif|jpg|png|css|htc|xml|txt))$ /exmpale.com/$1 [L]

RewriteCond %{HTTP_HOST} ^(www.)?exmpale.com$
RewriteRule .* /exmpale.com/index.php
幻想少年梦 2024-10-15 04:54:41

您没有捕获初始请求。尝试以下操作:

RewriteRule (.*)\.(woff|ttf|svg|js|ico|gif|jpg|png|css|htc|xml|txt)$ /MyFolder/$1.$2

这将获取以指定扩展名结尾的任何内容,并在 /MyFolder/ 下重定向,例如:/something/image.jpg 现在将转到 /MyFolder/something/image.jpg

注意: 通过添加条件来检查文件类型,可能有更好的方法来做到这一点。

You are not capturing the initial request. Try the following:

RewriteRule (.*)\.(woff|ttf|svg|js|ico|gif|jpg|png|css|htc|xml|txt)$ /MyFolder/$1.$2

This will get anything that ends in the extension specified and redirect under /MyFolder/ For example: /something/image.jpg will now go to /MyFolder/something/image.jpg

Note: There may be better ways to do this by adding conditions to check for file type.

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