views:

33

answers:

2
+1  Q: 

Regex help needed!

I'm working on a multilingual application that uses IIS7-based url rewriting.

I'd like the following Url actions:

1. fr-ca > index.aspx?l=&lc=fr-ca
2. fr-ca/ > index.aspx?l=&lc=fr-ca
3. fr-ca/568/sometitle > index.aspx?l=568&lc=fr-ca
4. 568/sometitle > > index.aspx?l=568&lc=

Essentially, the initial fr-ca is optional.

My current rule:

<match url="^(fr-ca.)?([^/][0-9]+)?/*" />

Fails on #1

Another attempt:

<match url="^(fr-ca)?(.[0-9]+)?/*" />

Passes all requirements, except the back reference {R:2} gives /568 in this case.

I suppose I could add another rule that adds a / to the end of a fr-ca only, but that doesn't seem right.

Thanks for any help! Regex drives me bonkers.

+1  A: 

Should you be doing it this way? Instead of seeing which url they hit (and redirecting to what I presume is a translated version of the page), you could actually check the Accept-Language header... probably even from within that ASP page.

This means that they see the language they want to see it in from the beginning, and without clicking on that dumb little flag at the top of the page. It doesn't rely on GeoIP, or user interaction.

Check it out.

John O
Do you have an answer to my question?
ScottE
A: 

Sorted it out myself.

^(fr-ca)?/?([0-9]+)?

passes:

fr-ca
fr-ca/
fr-ca/9
fr-ca/9/
fr-ca/9/sometitle
fr-ca/9/sometitle/
fr-ca/9/sometitle/anothertitle

9
9/
9/sometitle
9/sometitle/
9/sometitle/anothertitle
ScottE