ASP.net url rewrite force lowercase

人盡茶涼 提交于 2019-12-11 00:33:24

问题


I have:

<!-- Force lowercase URLS -->
<rewrite url="~/(.*[A-Z]+.*)$" to="~/handlers/permredirect.ashx?URL=${lower($1)}" />

Perm redirect simply 301 redirects to the new URL.

This rule is meant to redirect any URL with an uppercase char to the lower case one.

This however creates a redirect loop, any ideas why? The only rules running so far are:

<rewriter>

    <!-- Remove Trailing Slash for all URLS-->
    <rewrite url="~/(.*)/($|\?(.*))" to="~/handlers/permredirect.ashx?URL=${lower($1)}$2" />

    <!-- Force lowercase-->
    <rewrite url="~/(.*[A-Z]+.*)$" to="~/handlers/permredirect.ashx?URL=${lower($1)}" />

    <rewrite url="~/construct2($|\?(.*))" to="~/construct2.aspx" processing="stop" />
</rewriter>

回答1:


You can either modify the regular expression to exclude .ashx files (which might get extremely complicated) or create a new rule before this rule, that will catch URLs pointing to ashx files and redirect them to a lowercase version of the string.

Something like this might work (not tested):

<rewrite url="~/(?=(.*\.ashx.*))(.*[A-Z]+.*)" to="~/${lower($1)}" />

It uses a lookahead rule to check if ".ashx" is part of the url and if the URL is uppercase. If yes, it redirects to the lowercase version of the same url.



来源:https://stackoverflow.com/questions/7523539/asp-net-url-rewrite-force-lowercase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!