问题描述
我有一个与 Windows Server 2008 和 IIS7.5 共享的主机计划,并且安装并启用了 Microsoft 重写模块.
I have a shared hosting plan with Windows Server 2008 and IIS7.5, and there is Microsoft rewriting module installed and enabled.
<rewrite>
<rules>
<rule name="myRule" patternSyntax="Wildcard">
<!--Rewriting code-->
</rule>
</rules>
</rewrite>
那么,如何使用 Microsoft 重写模块将 mydomain.com/everywhere-in-site/my-page.html 重定向到 www.mydomain.com/everywhere-in-site/my-page.html?
So, how to redirect mydomain.com/everywhere-in-site/my-page.html to www.mydomain.com/everywhere-in-site/my-page.html with Microsoft rewriting module?
如果我想将 www.mydomain.com/everywhere-in-site/my-page.html 重定向到 mydomain.com/everywhere-in-site/my-page.html 怎么办?
And what if I want to redirect www.mydomain.com/everywhere-in-site/my-page.html to mydomain.com/everywhere-in-site/my-page.html ?
推荐答案
要从域中删除 www 并重定向到裸域",您可以像以下代码片段一样进行操作:
To remove the www from a domain and redirect to a "naked domain" you could di it like in the following code snippet:
<rewrite>
<rules>
<rule name="Remove WWW prefix" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.yourdomain.com$" />
</conditions>
<action type="Redirect" url="http://yourdomain.com/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
反过来(如果您愿意的话)将非 www 重定向到带有 www 的:
And the other way around (if you prefer that) to redirect a non-www to one with www:
<rewrite>
<rules>
<rule name="Add WWW prefix" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^yourdomain.com$" />
</conditions>
<action type="Redirect" url="http://www.yourdomain.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
redirectType="Permanent"
当然是可选的,但对于 SEO 和大多数情况我会推荐它.
The redirectType="Permanent"
is of course optional but for SEO and most scenarios I would recommend it.
另请参阅这些 SO 问题/答案:
Please see also these SO questions/answers:
- IIS7 URL 重写 - 添加www"前缀
- 转发 http://mydomain.com/ctrlr/act/val 到 http://WWW.mydomain.com/ctrlr/act/val
- 从地址中删除 www 的正确方法使用 IIS URL 重写
这篇关于Microsoft 重写模块 - 在 url 上强制 www 或从 url 中删除 www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!