views:

15

answers:

2

Hi All,

I am using IIS7 URL rewriting with my website which is written in ASP.NET. I am not sure the best way to deal with / in the rewritten URL because currently, when one is present it breaks the parameters because the engine thinks it needs to split it at the wrong place. For example, a url like:

www.test.com/myscript.aspx?code=dothis&title=tester/title

would be split incorrectly. "tester" would become the code and "title" the title. Is there any way I can prevent this or is it simply the case that you can't use /s because the engine splits the URL on them?

Thanks.

A: 

I have got around this by replacing all the forward slashes before they are used in the URL. I then re-enter the forward slash when I need it.

Not sure if this is the best solution but it is suitable for my usage.

webnoob
+1  A: 

The URLEncode function can be used to re-encode backreferences. For example:

^/myscript/([^/]+)/([^/]+)$                                    -> rewrite ->
/myscript.aspx?code={UrlEncode:{R:1}}&title={UrlEncode:{R:2}}

^/myscript.aspx?code=([^&]+)&title=([^&]+)$                    -> redirect ->
/myscript/{UrlEncode:{R:1}}/{UrlEncode:{R:2}}

When you're dealing with separate URL components, like single path parts or parameter values (which is the vast majority of the time), you will want to URLEncode; IMO it's a highly questionable bit of design that this doesn't happen (a) by default or (b) in the rules generated by Add Rule wizard.

bobince
I take it this is placed in the web.config file where all the rewrites are done?
webnoob
bobince
Thanks very much for the help.
webnoob