You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在IIS中重定向www.*格式URL的通用服务器级实现方案咨询

Server-Wide IIS Rewrite Rule to Strip www. from Subdomains

Got it, let's tackle this server-wide rewrite rule for stripping the www. prefix from all subdomains in IIS—no need to mess with each site's config individually. Here's how to do it properly:

Prerequisite: Install IIS URL Rewrite Module

First, make sure you have the IIS URL Rewrite Module installed on your server. If you don’t, open IIS Manager, go to the Web Platform Installer, search for "URL Rewrite", and install it—it’s a free, official Microsoft module.

Option 1: Set Up the Rule via IIS Manager (GUI)

This is the most straightforward way if you prefer clicking over editing config files:

  • Open IIS Manager, select your server node in the left Connections pane (the top-level node, not a specific site).
  • Double-click the URL Rewrite icon in the Features View.
  • Click Add Rule(s) in the right Actions pane.
  • Pick Blank Rule under Inbound Rules, then hit OK.
  • Configure the rule with these settings:
    • Name: Give it something descriptive like Strip WWW from All Subdomains
    • Pattern: Select "Regular Expressions" as the pattern type, then enter ^www\.(.*)$
    • Conditions: Click Add to create a condition:
      • Input: {HTTP_HOST}
      • Check if input string Matches the Pattern
      • Pattern: ^www\.(.*\..+)$ (this ensures we only target subdomains with at least two parts after www., e.g., sub.example.com instead of just www.example.com—adjust to ^www\.(.+)$ if you want to include the root domain too)
    • Action:
      • Action type: Redirect
      • Redirect URL: http://{C:1}{REQUEST_URI}
      • Redirect type: Permanent (301) (best for SEO, tells search engines the URL has moved permanently)
      • Check Append query string to keep any existing query parameters intact
  • Click Apply in the Actions pane to save the rule.

Option 2: Manually Edit the Server-Level Config File

If you prefer editing files directly, you can add the rule to your server’s applicationHost.config file (usually located at %windir%\System32\inetsrv\config\applicationHost.config):
Find the <system.webServer><rewrite><rules> section and add this rule inside it:

<rule name="Strip WWW from All Subdomains" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{HTTP_HOST}" pattern="^www\.(.*\..+)$" />
  </conditions>
  <action type="Redirect" url="http://{C:1}{REQUEST_URI}" redirectType="Permanent" appendQueryString="true" />
</rule>

Save the file, and IIS will pick up the change automatically (no need to restart the server in most cases).

How the Rule Works

  • The condition checks if the incoming request’s host starts with www. and has at least two domain parts after it (e.g., www.sub.example.com → captures sub.example.com as {C:1}).
  • The redirect uses the captured domain part ({C:1}) plus the original request path and query string ({REQUEST_URI}) to preserve the user’s intended destination.
  • A 301 redirect ensures that search engines and browsers cache the new URL, which is better for SEO and user experience.

Notes

  • If you need to handle HTTPS requests, duplicate the rule and change http:// to https://, or modify the action URL to dynamically use https:// if {HTTPS} is ON:
    <action type="Redirect" url="https://{C:1}{REQUEST_URI}" redirectType="Permanent" appendQueryString="true" />
    
  • Test the rule by visiting http://www.sub.example.com/your-page?param=test—it should redirect immediately to http://sub.example.com/your-page?param=test.

内容的提问来源于stack exchange,提问作者userSteve

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:14:03