Thursday, July 3, 2008

Apache Rewrite for underscore to hyphen

I have faced an url issue in my webapplication.

I was asked to rewrite the requests for the html pages which has "_" ,

Example :

http://mydomain.com/test/resource_with_underscore.html

(to)

http://mydomain.com/test/resource-with-underscore.html


the following rule will work fine for the above scenario,



RewriteRule ^/([^_]+)_([^_]+)_([^_]+)$ /$1-$2-$3 [L,R=301] #pages with 2 underscores
RewriteRule ^/([^_]+)_([^_]+)$ /$1-$2 [L,R=301] #pages with 1 underscore

Ref: http://underscorebleach.net/jotsheet/2005/04/perl-301-redirect-script

No comments: