开发者

Dealing with / in IIS URL rewrites

开发者 https://www.devze.com 2023-01-22 00:10 出处:网络
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 parameter

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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消