url rewriting - Issues Replacing underscores to hyphens with .htaccess -


although saw few posts thema, still having problems achieve want. old url's like:

http://myhostname.com/offer/1234_nice_offer_cool_whatever_hellyeah.htm 

and want rewrite them to:

http://myhostname.com/offer/1234-nice-offer-cool-whatever-hellyeah.htm 

this code tried in .htaccess. works fine replace underscores in url's don't have directory:

rewritecond %{request_uri} \.htm$ [nc]     rewriterule ^([^_]*)_+(.*)$ $1-$2 [e=underscores:yes,n]  rewritecond %{env:underscores} ^yes$ rewriterule ^(.*)$ http://%{http_host}/$1 [r=301,l] 

so, paire of rules work fine url's like:

http://myhostname.com/1234_some_section_hellyeah.htm 

but when try same code url's like:

http://myhostname.com/dir1/dir2/dir3/1234_some_offer_or_section.htm 

then, server makes infinite loop (i figure of [n] flag...) basically, know in way affects per-directory url these rules , why getting infinite loop. thanks!

you don't need n flag. replace underscore hyphen recursively following code work:

rewriterule ^([^_]+)_(.+?\.htm)$ $1-$2 [l,nc,e=underscores:yes]  rewritecond %{env:redirect_underscores} ^yes$ rewriterule ^([^_]+)$ /$1 [r=301,l] 

Comments