3

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

Barbaros Alp
  • 6,405
  • 8
  • 47
  • 61

3 Answers3

3

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.

Iain M Norman
  • 2,075
  • 15
  • 30
  • it didnt work out. But there is something that i forgot to mention about, the length of the string may be much more like Great-Property-For-People When i use the rules above my querystring gets PropertyId="Great-Estate-5-2" – Barbaros Alp Feb 22 '09 at 15:12
  • Just testing it. Found out it's not working quite right. So the number of dashes before the required ids is variable? – Iain M Norman Feb 22 '09 at 15:23
  • Great-Estate-5-2 Property Title=Great Estate | PropertyId=5 | LanguageId=2 – Barbaros Alp Feb 22 '09 at 15:32
  • There you go try the new one working for me. And for more than one digit now as well, as I assume your IDs will get to more than 10 :) – Iain M Norman Feb 22 '09 at 15:52
  • :) thank you very much, i have tried it and it s ok when i have 2 querystrings(propertyId and the Language) but if i have just only PropertyId querystring i get an error. Saying Source can not be found 404. What might be the reason :/ – Barbaros Alp Feb 22 '09 at 18:23
  • now my problem is with that url /Great-Property-10 if there is just one querystring there is a problem, but if there is two querystring it works great – Barbaros Alp Feb 22 '09 at 18:46
  • @Barbaros, take what teknohippy has and add a third rewrite rule to the end: – JasonMArcher Feb 22 '09 at 19:16
  • @Barbaros, for future reference in this case the 404 means that nothing was matched by any rule. – Iain M Norman Feb 22 '09 at 19:26
  • Thank you very much teknohippy for your help and JasonMArcher for the last golden shot :) adding as the third rule did the trick – Barbaros Alp Feb 22 '09 at 20:35
  • @teknohippy, could you please add this as the 3rd rule, i will mark it as answer. – Barbaros Alp Feb 22 '09 at 20:36
2

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>
Tomalak
  • 332,285
  • 67
  • 532
  • 628
1

This is the final solution that we have come 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>
  • 1st rule is about the file types which we don't need.
  • 2nd rule is about "if page gets 2 querystring"
  • 3rd rule is about if the page get only one querystring

Thank you very much for your helps teknohippy and JasonMArcher

Barbaros Alp
  • 6,405
  • 8
  • 47
  • 61
  • @Barbaros Alp: I think I misread your original question a bit, I somhow thought you had *three* parameters. You could collapse your 2nd and 3rd rule into one: – Tomalak Feb 23 '09 at 00:21
  • At least if you don't mind that the "Language" parameter would always be *there* in the end, though sometimes empty. – Tomalak Feb 23 '09 at 00:22
  • Oh, and I've updated my answer accordingly. Not that it would make much of a difference at this point, but I did not want to leave it incorrect. ;-) – Tomalak Feb 23 '09 at 00:42
  • Thank you very much Tomalak, i have checked your answer, really thank you very much for your help. – Barbaros Alp Feb 23 '09 at 00:49