asp.net - Web.Config Url 重写工作,但重定向不起作用
问题描述
我有以下 web.config 我在其中添加了 URL 重写和重定向规则。这是部分工作。
代码:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<caching enabled="false" />
<rewrite>
<rules>
<rule name="Rewrite to pages.asp">
<match url="^pages/([_0-9a-z-]+)" />
<action type="Rewrite" url="pages.asp?p={R:1}" />
</rule>
<rule name="Redirect from pages.asp">
<match url="^pages/([_0-9a-z-]+)" />
<action type="Redirect" url="pages/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
URL结构是这样的:
https://www.example.com/pages.asp?p=my-article
重写应该是这样的(并且有效):
https://www.example.com/pages/my-article/
如果我手动访问此 URL,它可以工作,但重定向部分无法正常工作。这段代码有问题。
解决方案
规则都写错了,通过使用 IIS 重写规则管理器,我现在有一个工作代码。这可能会帮助其他人解决同样的问题。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<caching enabled="false" />
<rewrite>
<rules>
<rule name="RedirectPages" stopProcessing="true">
<match url="^pages\.asp$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^p=([^=&]+)$" />
</conditions>
<action type="Redirect" url="pages/{C:1}" appendQueryString="false" />
</rule>
<rule name="RewritePages" stopProcessing="true">
<match url="^pages/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="pages.asp?p={R:1}" />
</rule>
</rules>
<outboundRules>
<rule name="OutboundPages" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)pages\.asp\?p=([^=&]+)$" />
<action type="Rewrite" value="{R:1}pages/{R:2}/" />
</rule>
</outboundRules>
</rewrite>
</system.webServer>
</configuration>
推荐阅读
- javascript - 我需要验证 coveo 搜索中的建议列表是否正确建议
- python - 通过 setuptools 进行特定于操作系统的安装
- python - 重构 if 语句
- java - 将 Gradle 项目导入 IntelliJ IDEA 时如何避免重复库
- linq - Automapper ProjectTo 不允许将属性映射到自定义方法
- javascript - JavaScript - 警报和确认框的事件侦听器?
- windows - Qt 上 MSCV17 的 CDB 调试器无法正常工作
- oracle - Oracle 应用程序 - 迁移到 Exadata 服务器
- sql - SQL Server:在选择列表中将列与别名相乘
- internet-explorer - Orbeon Safari 和 IE 中的垂直滚动条关闭自动完成菜单