如何重写URL(例如,website.com到www.website.com)?

时间:2013-05-06 01:06:29

标签: c# url url-rewriting

如何在每个页面上重写URL,如下所示:

  • website.comwww.website.com
  • website.com/page1.aspxwww.website.com/page1.aspx

2 个答案:

答案 0 :(得分:2)

好吧,如果我们正在谈论apache,那么在.htaccess

RewriteEngine   on
RewriteCond %{HTTP_HOST} ^website\.com [NC]
RewriteRule ^(.*)$ http://www.website.com/$1 [L,R=301]

对于IIS和web.config,请使用

<rewrite>
<rules>
    <rule name="Canonical Host Name" stopProcessing="true">
        <match url="(.*)" />
        <conditions logicalGrouping="MatchAll">
            <add input="{HTTP_HOST}" pattern="^www\.website\.com$" negate="true" />
        </conditions>
        <action type="Redirect" url="{MapSSL:{HTTPS}}www.website.com/{R:1}"    redirectType="Permanent" />
    </rule>
</rules>
<rewriteMaps>
    <rewriteMap name="MapSSL" defaultValue="OFF">
        <add key="ON" value="https://" />
        <add key="OFF" value="http://" />
    </rewriteMap>
</rewriteMaps>
</rewrite>

答案 1 :(得分:0)

IIS确实有一个重写模块,可以让你做你想做的事情http://www.iis.net/downloads/microsoft/url-rewrite

相关问题