【问题标题】:IIS URL Rewrite to querystring parameterIIS URL 重写为查询字符串参数
【发布时间】:2017-06-15 19:24:51
【问题描述】:
我需要捕获对不是文件夹的文件夹的请求,以将请求重定向到默认页面,并将请求的路径作为查询字符串。
例如,如果用户请求
www.mysite.com/IT
我希望它重定向到
www.mysite.com/default.aspx?q=IT
但是,如果他们请求的页面位于 real 子文件夹中,则不应重定向该页面。例如,对 www.mysite.com/projects/new.aspx 的请求不应尝试进行任何重定向,因为它是站点中的物理文件夹和文件。
【问题讨论】:
标签:
asp.net
iis
url-rewriting
【解决方案1】:
使用IIS Rewrite module 并在 web.config 中进行设置:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Redirector" patternSyntax="Wildcard">
<match url="*" />
<conditions>
<add input="{PATH_INFO}" pattern="/*" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/default.aspx?q={PATH_INFO}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
【解决方案2】:
您可以定义一个 404 错误页面,该页面将使用您想要的“q=”重定向到您的默认页面