在IIS中重定向www.*格式URL的通用服务器级实现方案咨询
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 afterwww., e.g.,sub.example.cominstead of justwww.example.com—adjust to^www\.(.+)$if you want to include the root domain too)
- Input:
- 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
- Name: Give it something descriptive like
- 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→ capturessub.example.comas{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://tohttps://, or modify the action URL to dynamically usehttps://if{HTTPS}isON:<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 tohttp://sub.example.com/your-page?param=test.
内容的提问来源于stack exchange,提问作者userSteve

