将 IIS7 url 重写部分移出 web.config 文件
我在 web.config 文件中创建了一个配置部分,其中包含如下所示的所有重写规则
<rewrite>
<outboundRules>
<rule name="OutboundRewriteCatalogURL" preCondition="ResponseIsHtml1">
<match filterByTags="A" pattern="^(.*/)Catalog\.aspx\?Catalog=([^=&]+)&(?:amp;)?Title=([^=&]+)$" />
<action type="Rewrite" value="{R:1}ctlg/{R:2}/{R:3}/" />
</rule>
<rule name="OutboundRewriteCategoryURL" preCondition="ResponseIsHtml1">
<match filterByTags="A" pattern="^(.*/)ProductList\.aspx\?Catalog=([^=&]+)&(?:amp;)?Category=([^=&]+)&(?:amp;)?Title=([^=&]+)$" />
<action type="Rewrite" value="{R:1}categ/{R:2}/{R:3}/{R:4}/" />
</rule>
<rule name="OutboundRewriteFullProductURL" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)Product\.aspx\?Catalog=([^=&]+)&(?:amp;)?Category=([^=&]+)&(?:amp;)?Product=([^=&]+)&(?:amp;)?Title=([^=&]+)$" />
<action type="Rewrite" value="{R:1}prd/{R:2}-{R:3}-{R:4}/{R:5}/" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
<rules>
<rule name="RedirectCatalogURL" stopProcessing="true">
<match url="^Catalog\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^Catalog=([^=&]+)&Title=([^=&]+)$" />
</conditions>
<action type="Redirect" url="Catalog/{C:1}/{C:2}" appendQueryString="false" />
</rule>
<rule name="RewriteCatalogURL" stopProcessing="true">
<match url="^ctlg/([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="Catalog.aspx?Catalog={R:1}&Title={R:2}" />
</rule>
<rule name="RedirectCategoryURL" stopProcessing="true">
<match url="^ProductList\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^Catalog=([^=&]+)&Category=([^=&]+)&Title=([^=&]+)$" />
</conditions>
<action type="Redirect" url="categ/{C:1}/{C:2}/{C:3}" appendQueryString="false" />
</rule>
<rule name="RewriteCategoryURL" stopProcessing="true">
<match url="^categ/([^/]+)/([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="ProductList.aspx?Catalog={R:1}&Category={R:2}&Title={R:3}" />
</rule>
<rule name="RedirectProductURL" stopProcessing="true">
<match url="^Product\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^Catalog=([^=&]+)&Category=([^=&]+)&Product=([^=&]+)&Title=([^=&]+)$" />
</conditions>
<action type="Redirect" url="prd/{C:1}-{C:2}-{C:3}/{C:4}" appendQueryString="false" />
</rule>
<rule name="RewriteProductURL" stopProcessing="true">
<match url="^prd/([^/]+)-([^/]+)-([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="Product.aspx?Catalog={R:1}&Category={R:2}&Product={R:3}&Title={R:4}" />
</rule>
</rules>
</rewrite>
它非常好并且工作良好,但我不想在 web.config 文件中包含所有这些内容,有没有办法进行重写配置在外部配置文件中?
I have made a config section in my web.config file that has all rewrite rules like the following
<rewrite>
<outboundRules>
<rule name="OutboundRewriteCatalogURL" preCondition="ResponseIsHtml1">
<match filterByTags="A" pattern="^(.*/)Catalog\.aspx\?Catalog=([^=&]+)&(?:amp;)?Title=([^=&]+)$" />
<action type="Rewrite" value="{R:1}ctlg/{R:2}/{R:3}/" />
</rule>
<rule name="OutboundRewriteCategoryURL" preCondition="ResponseIsHtml1">
<match filterByTags="A" pattern="^(.*/)ProductList\.aspx\?Catalog=([^=&]+)&(?:amp;)?Category=([^=&]+)&(?:amp;)?Title=([^=&]+)$" />
<action type="Rewrite" value="{R:1}categ/{R:2}/{R:3}/{R:4}/" />
</rule>
<rule name="OutboundRewriteFullProductURL" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)Product\.aspx\?Catalog=([^=&]+)&(?:amp;)?Category=([^=&]+)&(?:amp;)?Product=([^=&]+)&(?:amp;)?Title=([^=&]+)$" />
<action type="Rewrite" value="{R:1}prd/{R:2}-{R:3}-{R:4}/{R:5}/" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
<rules>
<rule name="RedirectCatalogURL" stopProcessing="true">
<match url="^Catalog\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^Catalog=([^=&]+)&Title=([^=&]+)$" />
</conditions>
<action type="Redirect" url="Catalog/{C:1}/{C:2}" appendQueryString="false" />
</rule>
<rule name="RewriteCatalogURL" stopProcessing="true">
<match url="^ctlg/([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="Catalog.aspx?Catalog={R:1}&Title={R:2}" />
</rule>
<rule name="RedirectCategoryURL" stopProcessing="true">
<match url="^ProductList\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^Catalog=([^=&]+)&Category=([^=&]+)&Title=([^=&]+)$" />
</conditions>
<action type="Redirect" url="categ/{C:1}/{C:2}/{C:3}" appendQueryString="false" />
</rule>
<rule name="RewriteCategoryURL" stopProcessing="true">
<match url="^categ/([^/]+)/([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="ProductList.aspx?Catalog={R:1}&Category={R:2}&Title={R:3}" />
</rule>
<rule name="RedirectProductURL" stopProcessing="true">
<match url="^Product\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^Catalog=([^=&]+)&Category=([^=&]+)&Product=([^=&]+)&Title=([^=&]+)$" />
</conditions>
<action type="Redirect" url="prd/{C:1}-{C:2}-{C:3}/{C:4}" appendQueryString="false" />
</rule>
<rule name="RewriteProductURL" stopProcessing="true">
<match url="^prd/([^/]+)-([^/]+)-([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="Product.aspx?Catalog={R:1}&Category={R:2}&Product={R:3}&Title={R:4}" />
</rule>
</rules>
</rewrite>
Its very nice and working good but I dont want to have all these stuff in the web.config file , is there a way to have rewrite configuration in an external config file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将配置拆分为多个文件,并使用 configSource 属性将各部分包含到主 web.config 文件中,例如:
web.config:
profile.config:
请参阅 此博文 或 此问题了解更多信息。
You can split your configuration into several files and include the parts into the main web.config file using the configSource attribute, e.g:
web.config:
profile.config:
See this blog post or this question for more information.