2016-05-17 60 views
1

代替下劃線,我需要改變以下網址htaccess的文件,刪除文件夾,並用破折號

http://somedomain.com/news/a_sample_news_article.html 

http://somedomain.com/post/a-sample-news-article 

我在我的htaccess其作品有這一點,但我相信它可以改進 - 有沒有人有更好的解決方案?

RewriteEngine on 
# replace underscores 
RewriteRule ^(news)/([^_]*)_+(.*)$ /$1/$2-$3 [L,NC,R=302] 
# redirect the directory from news to post 
RewriteRule ^news/(.*)$ /post/$1 [R,L] 
# remove .html from end of url 
RewriteRule ^(.*)\.html$ /$1 [L,R=301] 

任何幫助非常感謝!

回答

0

重定向

  • /新聞/ foo_bar這樣

  • /後/富吧

您可以使用以下規則:

RewriteEngine on 

# redirect "/news/foo_bar" to "/foo_bar"  
RewriteRule ^news/(.+)$ /$1 [L,R] 
#2 replace underscore with hypens 
RewriteRule (.*)_(.*) $1-$2 [N,E=uscores:yes] 
RewriteCond %{ENV:uscores} yes 
RewriteRule ^(.+)$ /post/$1 [L,R] 
+0

非常感謝回報 - 作品很棒。在這些規則的背景下,從原始網址中移除.html的最佳方式是什麼? – Newfoundland