(ISAPI_Rewrite) 当请求某个文件时重写域

发布于 2024-10-18 11:03:00 字数 321 浏览 1 评论 0原文

我正在尝试创建一个条件来重写 http://subdomain.example.com/foo 中的网址/foofile.txthttp://example.com/foo/foofile.txt 当请求中有 foo/foofile.txt 时。如果有人能指出我正确的方向,我将不胜感激任何和所有的帮助......

提前致谢, 乙

I am trying to create a condition to rewrite a url from http://subdomain.example.com/foo/foofile.txt to http://example.com/foo/foofile.txt when the request has foo/foofile.txt in it. If someone could point me in the right direction, I would appreciate any and all help...

Thanks in advance,
B

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

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

发布评论

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

评论(1

故笙诉离歌 2024-10-25 11:03:00

这是针对特定文件将特定子域重定向到主域。

您使用的是哪个版本的 isapi_rewrite,v2 还是 v3?

这里假设 v3:

RewriteCond %{HTTP:Host} ^subdomain\.(.*)$
RewriteRule ^(/foo/foofile3\.txt)$ http://%1$1 [NC,R=301]

RewriteCond 测试以子域(和点)开头的主机,并捕获剩余的(example.com)以方便重写规则。您也可以将特定的 example.com 放在那里,但这使其更加通用。

然后,RewriteRule 查找特定的 /foo/foofile3.txt 并捕获它(同样是为了方便不在规则中重复它)。结果是 %1 表示条件 (example.com) 中的捕获,$1 表示规则 (/foo/foofile3.txt) 中的捕获。

NC 不区分大小写,R=301 是永久重定向。

在 v3 中,查询字符串参数会自动处理,因此会保留文件后面的所有参数。

使其变得更加复杂的其他可能性是任何子域,或者文件名模式而不是一个特定的文件。

我在浏览器缓存我的尝试同时获得正确的规则时遇到了问题。因此,即使我更改了规则,浏览器也缓存了我之前的重定向。我的快速解决方法是计算新网址的文件名:foofile2、foofile3(我想我在第三次尝试时就得到了它......)。我也可以每次都清除浏览器缓存;这更快了。

This is redirecting a specific subdomain to the main domain, for a specific file.

Which version of isapi_rewrite are you using, v2 or v3?

This assumes v3:

RewriteCond %{HTTP:Host} ^subdomain\.(.*)$
RewriteRule ^(/foo/foofile3\.txt)$ http://%1$1 [NC,R=301]

The RewriteCond tests for a host beginning with subdomain (and a dot), and captures the remaining (example.com) for convenience in the rewrite rule. You could also put the specific example.com in there, but this keeps it more generic.

The RewriteRule then looks for a specific /foo/foofile3.txt, and captures it (again for convenience of not repeating it in the rule). The result has %1 for the capture up in the condition (example.com) and $1 for the capture in the rule (/foo/foofile3.txt)

The NC is not case-sensitive, the R=301 is a permanent redirect.

With v3, querystring parameters are automatically handled, so this keeps any that are after the file.

Other possibilities that make it a little more complex are any subdomain at all, or filename patterns instead of the one specific file.

I was having trouble with the browser caching my attempts while getting the rule right. So even though I changed the rule, the browser had cached my previous redirect. My quick fix was to count up the filename for new urls: foofile2, foofile3 (I guess I got it on the 3rd try...). I could have cleared the browser cache each time too; this was quicker.

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