Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewriting a Url which can contain 1 or 2 querystrings with URLRewriter.NET?

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

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

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

EDIT: and this page can get one of them or can get both of them, so my rewriter rule needs to handle both of them

So, i have set these rules to 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>

It is all OK if there is no Language querystring, but when there is a language querystring it gets the 3rd expression as the PropertyId instead of Language

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

Thanks

like image 957
Barbaros Alp Avatar asked Dec 06 '25 06:12

Barbaros Alp


2 Answers

Combined answer:

<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>

That's working well now for many combinations:

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

returns PropertyId=555 and Language=12.

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

returns PropertyId=666.

like image 168
Iain M Norman Avatar answered Dec 07 '25 21:12

Iain M Norman


Make the second parameter (the language value) optional in the match by adding a question mark:

Edit: this is a corrected version, made after I realized that I misunderstood the question a bit.

<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>

This is a cleaned-up and streamlined version of what the OP has working. It will produce output in the form of

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

Note

  • the use of the $0 back-reference to refer to the entire input string, without the need to actually match the entire input string
  • the use of non-capturing groups (?:...) for things we don't need to store in a match-group because we don't want to retrieve their value later
  • the collapse of the separate rules for single and double-argument URLs into a single rule

Original version of the answer:

<rewriter>
  <rewrite url="^/(.+?)-(.+?)-?(.+?)?\.aspx$" to="/PropertyDetail.aspx?PropertyId=$2&amp;#038;Language=$3" processing="stop"/>
</rewriter>
like image 34
Tomalak Avatar answered Dec 07 '25 22:12

Tomalak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!