URL Rewriting

I was wondering if anyone here has got some experience with URL rewriting in ASP or ASP.net. On my domain, I’ve got URLs like these : www.domain.com/photography/album/?gid=1&aid=2 , but I’d like to rewrite this to something like www.domain.com/photography/1/2 , though both ways should point to the same location.

Can anyone help me? The tutorials I’ve found on the Net aren’t very clear…

Thanks in advance !

Regards,
Niels

If it is ASP.NET your using take a look at HTTP Handlers and HTTP Modules. They will be able to help you. I fyou can’t find anything lte me know and I will post a sample.

Check the article about url rewriting for asp.net from Microsoft:
ASP.NET Core, an open-source web development framework | .NET

If you need to use postback function, you need to do a little bit more. The links at the bottom of the acticle have some discussion.

Yeah, I read that article today and downloaded the source. It would 've been the ideal solution for what I need in fact. Though, it doesn’t work. I let the web project reference to both “URLRewriter” and “ActionLessForm”, and registered the tagprefix in the form, but accessing a page over localhost/domain/photography/1/2/45.aspx gives an error 404.

Is the structure of the web.config correct? Or did I do something very wrong? Do I need to change settings in IIS?

<configSections><section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" /></configSections> <RewriterConfig> <Rules> <RewriterRule> <LookFor>~/photography/(\d{2})/(\d{2})\.aspx</LookFor> <SendTo>~/photography/album/default.aspx?gid=$1&aid=$2</SendTo> </RewriterRule> <RewriterRule> <LookFor>~/photography/(\d{2})/(\d{2})/(\d{2})\.aspx</LookFor> <SendTo>~/photography/photo/default.aspx?gid=$1&aid=$2&pid=$3</SendTo> </RewriterRule> </Rules> </RewriterConfig> <system.web> <httpHandlers> <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" /> </httpHandlers> ...

Thanks in advance for everyone’s help !

Regards,
Niels

I beleive /(d){2} will only match numbers 2 characters long. If you are trying to do /1/4/45 or similar it will not recognise it. you could try /(d)*

That should be (\d+) then, everything inside the parentheses to capture the entire number, and + to match at least 1 digit, and not also 0 which probably doesn’t make sense.

OK, changed that d{2} to d+, however, i still have a 404 error.

web.config now loooks like this : I suppose there’s something wrong in the item httpHandlers…

<configSections> <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" /> </configSections> <RewriterConfig> <Rules> <RewriterRule> <LookFor>http://localhost/ArsLuminis/photography/(\d+)/(\d+)\.aspx</LookFor> <SendTo>http://localhost/ArsLuminis/photography/album/default.aspx?gid=$1&aid=$2</SendTo> </RewriterRule> <RewriterRule> <LookFor>http://localhost/ArsLuminis/photography/(\d+)/(\d+)/(\d+)\.aspx</LookFor> <SendTo>http://localhost/ArsLuminis/photography/photo/default.aspx?gid=$1&aid=$2&pid=$3</SendTo> </RewriterRule> </Rules> </RewriterConfig> <system.web> <httpHandlers> <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" /> </httpHandlers>