我有一个与
Windows Server 2008和IIS7.5的共享托管计划,并且安装并启用Microsoft重写模块.
<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?
如果我要将www.mydomain.com/everywhere-in-site/my-page.html重定向到mydomain.com/everywhere-in-site/my-page.html怎么办?
解决方法
要从域中删除www并重定向到“裸域”,您可以像下面的代码片段一样:
<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:
<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 =“永久”当然是可选的,但是对于SEO和大多数场景,我会推荐它.
请参阅这些问题/答案:
> IIS7 URL Rewrite – Add “www” prefix
> Forwarding http://mydomain.com/ctrlr/act/val to http://WWW.mydomain.com/ctrlr/act/val
> Proper method to remove www from address using IIS URL Rewrite