0

I have a website that was converted from 1 platform to another. I have many thousands of pages indexed that look like this:

domain.com/test/red_widget.html

domain.com/test/big_red_widget.html

domain.com/test/small_nice_red_widget.html

that need to look like this:

domain.com/test/red-widget.html

domain.com/test/big-red-widget.html

domain.com/test/small-nice-red-widget.html

So all the URLs in question in this example are located in the "test" path, use underscores and have an html extension.

And I need all urls in the "test" path with an html extension to be rewritten as is except the underscores, "_", would be replaced with dashes, "-".

I've tried a lot of stuff but I can get compatibility with my current htaccess rules which need to continue to work.

Right now I have this rule:

RewriteRule ^test/(.*).html$ /?kw=$1

which does work but for a url like domain.com/test/small_nice_red_widget.html, the kw parameter will be set as small_nice_red_widget whereas I really need it to be set as small-nice-red-widget.

Any guidance or if anyone can point me in the right directions will be greatly appreciated.

Bogarto
  • 37
  • 5
  • Possible duplicates: http://stackoverflow.com/questions/2105009/mod-rewrite-replace-all-underscore-with-hyphen, http://stackoverflow.com/questions/1279681/mod-rewrite-replace-underscores-with-dashes, http://stackoverflow.com/questions/2637004/how-do-i-use-htaccess-rewriterule-to-change-underscores-to-dashes – goodeye May 25 '14 at 17:03

2 Answers2

0

This should work for you:

RewriteRule ^test/(.*)_(.*)_(.*)_(.*).html$ /test/$1-$2-$3-$4.html [R,L]
RewriteRule ^test/(.*)_(.*)_(.*).html$ /test/$1-$2-$3.html [R,L]
RewriteRule ^test/(.*)_(.*).html$ /test/$1-$2.html [R,L]

Place the above, just above your current rewrite rule.

Howli
  • 12,291
  • 19
  • 47
  • 72
0

You can also do this to replace an arbitrary number of underscores with slashes:

RewriteRule ^test/(.*)_(.*)\.html$ /test/$1-$2.html [L,E=DASH:Y]
RewriteCond %{ENV:DASH} Y
RewriteRule ^([^_]+)$ /$1 [L,R=301]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220