如何在IIS规则中移除子文件夹?站点迁移后规则配置求助
Hey there! Let's sort out that IIS rewrite rule issue you're facing. Since your new site's URL structure mirrors the old one exactly—just without the subfolder1 segment—we can build a targeted rule to fix this properly.
subfolder1 First off, make sure you have the IIS URL Rewrite Module installed on your server. This is non-negotiable for the rule to work—if it's missing, you can grab it via the Web Platform Installer in IIS Manager.
Next, add or update the <rewrite> section in your new site's root web.config file with this rule:
<configuration> <system.webServer> <rewrite> <rules> <rule name="Remove subfolder1" stopProcessing="true"> <!-- Match URLs starting with subfolder1/ --> <match url="^subfolder1/(.*)$" /> <!-- Internally route to the part after subfolder1 --> <action type="Rewrite" url="{R:1}" /> <!-- Optional: Uncomment below to redirect (updates browser URL to clean version) --> <!-- <action type="Redirect" url="{R:1}" redirectType="Permanent" /> --> </rule> </rules> </rewrite> </system.webServer> </configuration>
Quick breakdown of how this works:
match url="^subfolder1/(.*)$": This regex targets any incoming URL that starts withsubfolder1/, capturing everything after that segment into the{R:1}variable.action type="Rewrite": This maps the request internally to the clean URL (nosubfolder1) without changing what the user sees in their browser. If you want the address bar to update to the simplified URL (great for SEO), switch toRedirectwithredirectType="Permanent"(a 301 redirect).
Troubleshooting if it's still not working:
- Confirm the URL Rewrite Module is enabled for your specific site in IIS Manager.
- Ensure your
web.configis placed in the root ofwww.mycoproduct.com, not in a subfolder. - Test with a sample URL: If someone visits
www.mycoproduct.com/subfolder1/about.html, it should loadwww.mycoproduct.com/about.htmlseamlessly. - Use the Test Pattern tool in the IIS URL Rewrite module to debug whether your regex is matching URLs correctly.
If you need to cover cases where subfolder1 doesn't have a trailing slash (like www.mycoproduct.com/subfolder1page), adjust the match regex to ^subfolder1(.*)$ instead.
内容的提问来源于stack exchange,提问作者jimmyd40

