0

我需要创建一个 301 重定向规则,该规则将匹配/替换下划线 _ 与破折号 - 并删除尾随 .html。URL 可以有任意数量的下划线 _ 这对我来说很困难。

在 PHP 中,我可以这样做:

$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'

我将如何在.htaccess 中写这个?

谢谢您的帮助。

4

1 回答 1

1

试试这个

  Options +FollowSymLinks -MultiViews
  RewriteEngine on
  RewriteCond %{REQUEST_URI} ^(.*?)_(.*?)$ [NC]
  RewriteRule ^  /%1-%2 [R,L]
  RewriteCond %{REQUEST_URI} ^(.*?).html$ [NC]
  RewriteRule ^  /%1 [R,L]
于 2013-02-15T20:09:54.380 回答