2013-02-15 63 views
0

我需要創建一個301重定向規則,它將匹配/用下劃線替換下劃線_並刪除尾部的.html。這些URL可以有任意數量的下劃線_這對我來說很困難。將PHP preg_replace轉換爲htaccess重寫

在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?

感謝您的幫助。

+0

@約翰我真的不知道從哪裏在一個正則表達式開始使用多種替代:/ – user2076817 2013-02-15 20:06:50

回答

1

試試這個

Options +FollowSymLinks -MultiViews 
    RewriteEngine on 
    RewriteCond %{REQUEST_URI} ^(.*?)_(.*?)$ [NC] 
    RewriteRule^/%1-%2 [R,L] 
    RewriteCond %{REQUEST_URI} ^(.*?).html$ [NC] 
    RewriteRule^/%1 [R,L] 
相關問題