Rewriting Url that can contain 1 or 2 requests with URLRewriter.NET?

In my project, my /PropertDetail.aspx can get 2 requests.

1st for PropertyId/PropertDetail.aspx?PropertyId=5

2nd for language/PropertDetail.aspx?PropertyId=5&Language=2

EDIT: and this page can get one of them or it can get both of them, so my override rule should handle both of them

So, I set these rules in web.config

<rewriter>
        <rewrite url="^(/.+(\.gif|\.png|\.jpg|\.ico|\.pdf|\.css|\.js)(\?.+)?)$" to="$1" processing="stop" />
        <rewrite url="^/(.+)-(.+).aspx$" to="/PropertyDetail.aspx?PropertyId=$2" processing="stop"/>
        <!--http://localhost:1562/Harika-Gayrimenkul-5.aspx-->
        <rewrite url="^/(.+)-(.+)-(.+).aspx$" to="/PropertyDetail.aspx?PropertyId=$2&#038;Language=$3" processing="stop"/>
        <!--http://localhost:1562/Great-Property-5-2.aspx-->
</rewriter>

Everything is fine, if there is no language sequence, but if there is a language query, it receives the third expression as PropertyId instead of the language

How can I define these two rules for the same page?

thanks

+3
3

:

<rewriter>
    <rewrite url="^(/.+(\.gif|\.png|\.jpg|\.ico|\.pdf|\.css|\.js)(\?.+)?)$" to="$1" processing="stop"/>
    <rewrite url="^.+?([\d]+?)-([\d]+?)\.aspx$" to="/PropertyDetail.aspx?PropertyId=$1&amp;Language=$2" processing="stop"/>
    <rewrite url="^.+?-([\d]+?)\.aspx$" to="/PropertyDetail.aspx?PropertyId=$1" processing="stop"/>

</rewriter>

:

/This-is-a-really-long-property-title-555-12

PropertyId = 555 Language = 12.

/This-is-another-really-long-property-title-666

PropertyId = 666.

+3

( ) , :

: , , , .

<rewriter>
  <rewrite url="\.(?:gif|png|jpg|ico|pdf|css|js)(?:\?.*)?$" to="$0" processing="stop"/>
  <rewrite url="(\d+)(?:-?(\d+)?)\.aspx$" to="/PropertyDetail.aspx?PropertyId=$1&amp;Language=$2" processing="stop"/>
</rewriter>

, OP.

/PropertyDetail.aspx?PropertyId=12345&Language=1   (when language is present)
/PropertyDetail.aspx?PropertyId=12345&Language=    (when it isn't)

  • $0
  • - (?:...) , ,
  • URL-

:

<rewriter>
  <rewrite url="^/(.+?)-(.+?)-?(.+?)?\.aspx$" to="/PropertyDetail.aspx?PropertyId=$2&amp;#038;Language=$3" processing="stop"/>
</rewriter>
+2

This is the final decision that we came up with.

<rewriter>
    <rewrite url="^(/.+(\.gif|\.png|\.jpg|\.ico|\.pdf|\.css|\.js)(\?.+)?)$" to="$1" processing="stop"/>
    <rewrite url="^.+?([\d]+?)-([\d]+?)\.aspx$" to="/PropertyDetail.aspx?PropertyId=$1&amp;Language=$2" processing="stop"/>
    <rewrite url="^.+?-([\d]+?)\.aspx$" to="/PropertyDetail.aspx?PropertyId=$1" processing="stop"/>
</rewriter>
  • The first rule concerns file types that we do not need.
  • The second rule is "if the page receives a 2 query string"
  • 3rd rule: if the page has only one request

Thanks so much for your teknohippy and JasonMArcher tips.

+1
source

Source: https://habr.com/ru/post/1703648/


All Articles