ASP.NET web application中的redirect
在開發ASP.NET MVC web application過程中,開發上線了新系統後,需要把老系統的url redirect新系統下
其中在項目系統目錄下有一個文件 301RedirectsPages.config, 內容如下:
<rewriteMaps> <rewriteMap name="Redirects"> <add key="/contact-us.aspx" value="/contact-us" /> <add key="/services-solutions" value="/services-and-solutions"/> <add key="/sales-support" value="/sales-and-support" /> <add key="/events-news" value="/events-and-news" /> <add key="/services-solutions/services.aspx" value="/services-and-solutions/services" /> <add key="/services-solutions/solutions.aspx" value="/services-and-solutions/solutions"/> </rewriteMap> </rewriteMaps>
在web.config文件中,在system.webServer下面,有一個rewrite節點,顯示如下:
<system.webServer> <rewrite xdt:Transform="Replace"> <rewriteMaps configSource="301RedirectsPages.config" /> <rules> <rule name="Redirect to HTTPS"stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTPS}" pattern="^OFF$" /> </conditions> <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" /> </rule> <rule name="Remove trailing slash" stopProcessing="true"> <match url="(.*)/$" /> <conditions> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Redirect" redirectType="Permanent" url="{R:1}" /> </rule> <rule name="Redirects"> <match url=".*" /> <conditions> <add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" /> </conditions> <action type="Redirect" url="{C:1}" appendQueryString="true" /> </rule> </rules> </rewrite> </system.webServer>
這種情況下,在瀏覽器窗口輸入URL:
localhost/contact-us.aspx 將自動redirect到 localhost/contact-us
localhost/services-solutions/services.aspx 自動redirect到 localhost/services-and-solutions/services
localhost/services-solutions/solutions.aspx 自動redirect到 localhost/services-and-solutions/solutions
但是,當你輸入
localhost/services-solutions 不能自動redirect到 localhost/services-and-solutions
localhost/sales-support 不能自動redirect到 localhost/sales-and-support
localhost/events-news 不能自動redirect到 localhost/events-and-news
查找了半天的原因,才發現是在web.config 中的設置
appendQueryString="true" 應該改成 appendQueryString="false"
也就是這一行 <action type="Redirect" url="{C:1}" appendQueryString="true" />
應該改成 <action type="Redirect" url="{C:1}" appendQueryString="false" />
改成後的web.config文件如下:
<rewrite xdt:Transform="Replace"> <rewriteMaps configSource="301RedirectsPages.config" /> <rules> <rule name="Redirect to HTTPS" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTPS}" pattern="^OFF$" /> </conditions> <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" /> </rule> <rule name="Remove trailing slash" stopProcessing="true"> <match url="(.*)/$" /> <conditions> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Redirect" redirectType="Permanent" url="{R:1}" /> </rule> <rule name="Redirects"> <match url=".*" /> <conditions> <add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" /> </conditions> <action type="Redirect" url="{C:1}" appendQueryString="false" /> </rule> </rules> </rewrite>
ASP.NET web application中的redirect