IIS重写规则改变了我当前的aspx页面超链接href错误的方式 [英] IIS rewrite rule change my current aspx page hyperlink href wrong way

查看:193
本文介绍了IIS重写规则改变了我当前的aspx页面超链接href错误的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从本地iis浏览我的网站时,地址在浏览器地址栏中显示为



所以在测试期间我看到 {R:1}是/ gb /而{R:2}是default.aspx 。因此,当此规则执行时,更改 default.aspx页面中的所有超链接href ,并且所有超链接href现在变为 http:// localhost:8800 / gb / default.aspx



基本上我需要注入当前页面的所有超链接href中的浏览器地址栏网址的国家/地区代码



我想我更接近什么我想要实现,但现在我需要一些帮助来解决这个问题。我想这个区域需要改变位< action type =Rewritevalue ={R:1} {R:2}/>



所以请一些帮助,让我朝着正确的方向前进。

解决方案

你的重写当您使用 {R:1} {R:2} 但是这将永远不会匹配,因为你用 negate =true来否定它。即如果它与正则表达式不匹配, {R:1} {R:2} 将(大多数)可能)也不可用。



您需要做的是添加一个与查询字符串匹配的具有相同正则表达式的额外条件。此条件将确保当前URL与该正则表达式匹配,然后您可以使用 {C:1} {返回引用其中的部分内容C:2} 等......(字母 C 而不是 R )。诀窍是使用 {C:1} {R:0} 的组合。 {C:1} 将为您提供当前网址的语言代码, {R:0} 将为您提供来自< a href> 的原始网址。



所以你最终得到:

 < outboundRules> 
< rule name =add outbound rulepreCondition =Ishtmlenabled =truestopProcessing =true>
< match filterByTags =Apattern =^ \ /(?![a-z] {2} \ /).*/>
< action type =Rewritevalue ={C:1} {R:0}/>
< conditions>
< add input ={HTTP_X_ORIGINAL_URL}pattern =^(\ / [a-z] {2})\ /(.*)/>
< / conditions>
< / rule>
< preConditions>
< preCondition name =Ishtml>
< add input ={RESPONSE_CONTENT_TYPE}pattern =text / html/>
< / preCondition>
< / preConditions>
< / outboundRules>

在旁注中,A标签的正则表达式非常弱。它将匹配 /gb/default.aspx 但它也将匹配 /foo/bar/default.aspx /foo/bar/gb/default.aspx 因此不会重写这些链接。我用更严格的版本替换它只匹配 /<两个字母> /<任何>



<请注意,我在条件中取得了第一组中前两个字母匹配后的 \ / ,因为我们不需要在后面使用斜杠参考 {C:1} ,因为它已经在 {R:0}



编辑:我假设 {R:0} 仍然可用作后向参考,即使正则表达式不匹配。似乎并非如此。所以我拿出 negate =true并修正了正则表达式以匹配任何URL,除了那些以 /< two-letters / <开头的URL / code>(以防止重写已经正确的URL)。



另一个错误是匹配 {REQUEST_URI} 假设这是浏览器中显示的原始URL,其中包含语言代码。但最明显的不是你可能(入站)重写该URL并取出语言代码。所以我用 {HTTP_X_ORIGINAL_URL} 替换了它。这是由URL Rewrite模块设置的变量,并保留原始URL。



最后< add ..> <条件> ...< / condition> 中缺少结算 / 。我希望这对你有用。


when browse my site from local iis then address looks in browser address bar as http://localhost:8800/gb/default.aspx

i tried to extract country code from browser address bar and injected in all hyperlink's href with IIS rewrite outbound rule.

this is my outbound rule i used in my web.config file.

<outboundRules>
    <rule name="add outbound rule" preCondition="Ishtml" enabled="true" stopProcessing="true">
        <match filterByTags="A" pattern="(\/[a-z]+\/)(.*)" negate="true" />
        <action type="Rewrite" value="{R:1}{R:2}" />
    </rule>
    <preConditions>
        <preCondition name="Ishtml">
            <add input="{RESPONSE_CONTENT_TYPE}" pattern="text/html" />
        </preCondition>
    </preConditions>
</outboundRules>

when i pattern test from iis rewrite module window then output looks like below one. here is screen shot.

so during test i saw {R:1} is /gb/ and {R:2} is default.aspx. so when this rule execute it change all hyperlink href in default.aspx page and all hyperlink href becomes now http://localhost:8800/gb/default.aspx

basically i need to inject country code from browser address bar url in all hyperlink href of current page.

i think i am bit closer to what i am trying to achieve but now i need little help to sort this issue. i guess this area need to be change bit <action type="Rewrite" value="{R:1}{R:2}" />

so please some help and drive me to right direction.

解决方案

Your rewrite action will not work as you back reference to parts of the match of the rule with {R:1} and {R:2} but this will never match as you negated it with negate="true". I.e. if it doesn't match that regular expression the {R:1} and {R:2} will (most likely) also not be available.

What you have to do is add an extra condition that matches on the query string with the same regular expression. This condition will make sure that the current URL will match that regular expression and then you can back reference parts of that with {C:1} and {C:2}, etc... (with the letter C instead of R). The trick is to use a combination of {C:1} and {R:0}. {C:1} will give you the language code from the current URL and {R:0} will give you the original URL from the <a href>.

So you will end up with:

<outboundRules>
    <rule name="add outbound rule" preCondition="Ishtml" enabled="true" stopProcessing="true">
        <match filterByTags="A" pattern="^\/(?![a-z]{2}\/).*" />
        <action type="Rewrite" value="{C:1}{R:0}" />
        <conditions>
            <add input="{HTTP_X_ORIGINAL_URL}" pattern="^(\/[a-z]{2})\/(.*)" />
        </conditions>
    </rule>
    <preConditions>
        <preCondition name="Ishtml">
            <add input="{RESPONSE_CONTENT_TYPE}" pattern="text/html" />
        </preCondition>
    </preConditions>
</outboundRules>

On a side note, your regular expression for the A tag is very weak. It will match /gb/default.aspx but it will also match /foo/bar/default.aspx or /foo/bar/gb/default.aspx and thus not rewrite these links. I've replaced it with a more strict version only matching /<two letters>/<anything>.

Note that I took the \/ after the match for the first two letters out of the first group in the condition as we don't need that slash in the back reference {C:1} as it's already in {R:0}.

EDIT: I made the assumption that {R:0} would still be available as a back reference even if the regular expression would not match. This does not seem to be the case. So I took out the negate="true" and fixed the regular expression to match any URL except those starting with /<two-letters/ (to prevent rewriting already correct URL's).

Another error was to match on {REQUEST_URI} assuming this was the original URL as seen in the browser with the language code in it. But most obviously it's not as you probably (inbound) rewrite that URL and take out the language code. So I replaced that with {HTTP_X_ORIGINAL_URL}. This is a variable set by the URL Rewrite module and preserves the original URL.

Lastly the <add ..> in the <condition>...</condition> was missing a closing /. I hope this now works for you.

这篇关于IIS重写规则改变了我当前的aspx页面超链接href错误的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆