0

I need to create a 301 redirect rule that will match/replace underscores _ with dashes - and remove the trailing .html. The URLs could have any number of underscores _ which is making this difficult for me.

In PHP I can do this as such:

$subject = 'this_is_a_bad_url.html';    
$pattern = array('/(_)/', '/.html/');
$replace = array('-', '');
$output = preg_replace($pattern, $replace, $subject);
//$output would result to 'this-is-a-bad-url'

How would I write this in .htaccess?

Thanks for the help.

1
  • @John I really don't know where to start using multiple replacements in one regex :/ Commented Feb 15, 2013 at 20:06

1 Answer 1

1

try this

  Options +FollowSymLinks -MultiViews
  RewriteEngine on
  RewriteCond %{REQUEST_URI} ^(.*?)_(.*?)$ [NC]
  RewriteRule ^  /%1-%2 [R,L]
  RewriteCond %{REQUEST_URI} ^(.*?).html$ [NC]
  RewriteRule ^  /%1 [R,L]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.