redirect - Windows Azure : redirect website from www to non-www

标签 redirect azure iis web-config url-rewriting

我的服务器上有这个 web.config 文件。

<rewrite>
   <rules>

      <rule name="HTTP to HTTPS redirect" stopProcessing="true">
         <match url="(.*)" />
         <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
         </conditions>
         <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
      </rule>

      <rule name="Rule" stopProcessing="true">
         <match url="^(.*)$" ignoreCase="false" />
         <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
            <add input="{URL}" pattern="^/favicon.ico$" ignoreCase="false" negate="true" />
         </conditions>
         <action type="Rewrite" url="index.php/{R:1}" appendQueryString="true" />
      </rule>

   </rules>
</rewrite>

我想将 URL 从 www 重定向到非 www。例如,如果用户输入 www.exmaple.com,则应转到 https://example.com

我怎样才能在上面的代码中编辑这样的东西。谢谢。

最佳答案

你可以这样(我已将 https/非 www 规则合并到一条规则中)

<rule name="HTTPS and non-WWW only" stopProcessing="true">
    <match url="^(.*)$" />
    <conditions logicalGrouping="MatchAny">
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
        <add input="{HTTP_HOST}" pattern="^www\." ignoreCase="true" />
    </conditions>
    <action type="Redirect" redirectType="Found" url="https://example.com/{R:1}" />
</rule>

<rule name="Generic default rule" stopProcessing="true">
    <match url="^(.*)$" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_URI}" pattern="^/favicon\.ico$" ignoreCase="true" negate="true" />
    </conditions>
    <action type="Rewrite" url="/index.php/{R:1}" appendQueryString="true" />
</rule>

关于redirect - Windows Azure : redirect website from www to non-www,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29949986/

相关文章:

regex - 不带 .html 后缀的 URL 重定向

java - HTTPURLConnection 不遵循从 HTTP 到 HTTPS 的重定向

asp.net - 在 IIS 中部署 Angular 6 应用程序后,我收到一个空白页面,没有错误

iis - 是否可以使 IIS 上的站点的一部分只能从本地主机查看?

sql-server - Azure WebApp 到虚拟机 SQL 服务器的连接速度很慢

redirect - 两个可以用来点击首页的网址

javascript - 使用 POST 请求重定向的好方法?

azure - 从 Azure HDInsights 中的分区 parquet 文件创建 Hive 外部表

c# - Azure 连接设置中的 Web API 因大请求而失败

mysql - 基于 Linux 的 Azure PHP 应用程序服务缺少 MYSQL 驱动程序