This project is read-only.

Rewriting an ashx handler

Jun 27, 2011 at 4:26 PM

An old site that we're converting to Orchard had an ashx handler registered. The URL looked something like this:

I need to preserve that route in my Orchard site but rewrite it to the actual Orchard route like this:

Here's what my RewriteRule looks like:
RewriteRule ^oldfoo.ashx?bar=(\w+) /newfoo?bar=$1 [R,NC]

However, when I try to hit the old route on the Orchard site I get a 404. I have some other rewrite rules that are working successfully so I know the module is working. Is there something about the ashx that's preventing this one from working? Is it because IIS or ASP.Net is trying to handle it before it gets to Orchard?

Jun 27, 2011 at 5:02 PM

What version of IIS ? I know IIS express doesn't handle everything by default, and needs some extensions to be registered.
Also, maybe using $ at the end of the regular expression may help, and be careful, the ? in the middle of the regex should be escaped too.

Jun 27, 2011 at 5:20 PM

This is on the full version of IIS on a Win 7 machine (so that's 7.5 I think?) I tried escaping the ?'s and adding the $ to the end like this:

RewriteRule ^oldfoo.ashx\?bar=(\w+)$ /newfoo\?bar=$1 [R,NC]

But that didn't work either. I also tried it just like that but without escaping the ? after "newfoo" in the rewritten url (since I wasn't sure if that part is treated like a regex, other than the variable replacement), but that didn't help. Any other ideas? I could just write a simpler handler to have it do the redirect and drop it in the Orchard.Web folder, but I was hoping to be able to use the Rewrite Rules module so I don't have that extra code floating out there on it's own.

Jun 27, 2011 at 7:46 PM

The query string can't be used in Rewrite rules, the regular expression only work on the path part. You need a condition to do it, like this one:

RewriteCond %{QUERY_STRING} bar=(.+)
RewriteRule ^oldfoo.ashx newfoo?bar=%1

BTW, I will update the module with a Simulation page.

Jun 27, 2011 at 8:07 PM

Ah, I didn't know about RewriteCond. That worked perfectly. Thanks Sebastien ... you da man! :-)

Jun 27, 2011 at 9:20 PM

Module updated. You can enjoy the new Simulations page, right after the settings tab.