Underscores to hyphens, but not after "?"

ISAPI_Rewrite is Apache mod_rewrite compatible URL rewriter for Microsoft IIS
User avatar
Posts: 1
Joined: 03 May 2013, 14:49

Underscores to hyphens, but not after "?"

03 May 2013, 14:55

I need to replace underscores “_” with hyphens “-“ in URLs using ISAPI_Rewrite (for SEO reasons). I only need to replace the underscores in the true URL section of the incoming request, not in any parameters which would come after a question mark “?”. For example the request for “http://www.myurl.com/about_us?parameter_with_underscores=123” needs to be rewritten as “http://www.myurl.com/about-us?parameter_with_underscores=123”. My rewrite rule also needs to cover parameter-less requests such as “http://www.myurl.com/about_us” being rewritten to “http://www.myurl.com/about-us”. Every time I make a rule specific enough to not mess with the parameters it ends up not working on the parameter-less variant.
Here’s an example (not good enough) rule I tried:
Rewrite ^(.*)_(.*)(?=.*?\?)(.*) [L,R=301]
Any insights and hints from experienced users would be appreciated.

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: Underscores to hyphens, but not after "?"

03 May 2013, 19:02

Hello,

The solution would be very simple - QSA-flag. It automatically appends querystring to the landing URL.
Code: Select all
Rewrite ^([^_]+)_(.*) $1-$2 [NS,QSA,R=301,L]


Regards
Andrew

Return to ISAPI_Rewrite 3.0

Who is online

Users browsing this forum: No registered users and 21 guests