2016-05-17 58 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