阿里云虚拟主机之301跳转

今天客户咨询要做301跳转,不带WWW的网页,默认全转向带www的。

主机面板中就有此功能,但咨询阿里云技术下来得到的结论是:
301跳转只适合首页。

如果代码实现:
NT主机。配置web.config代码:

<rewrite>
  <rules>
    <rule name="Redirect to www" stopProcessing="true">
      <match url="(.*)" />
      <conditions trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="^domain.com$" />
      </conditions>
      <action type="Redirect"
        url="{MapProtocol:{HTTPS}}://www.domain.com/{R:1}" />
    </rule>
  </rules>
  <rewriteMaps>
    <rewriteMap name="MapProtocol">
      <add key="on" value="https" />
      <add key="off" value="http" />
    </rewriteMap>
  </rewriteMaps>
</rewrite>

上面只适合.NET页面。
静态页无用。
后帮客户用JS实现了。
代码如下:

if( !/\.?www./g.test(location.host) ) location.href = location.href.replace("://","://www.")