htaccess文件删除文件夹,并用短划线替换下划线

时间:2016-05-17 14:33:45

标签: .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] 

非常感谢任何帮助!

1 个答案:

答案 0 :(得分: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]
相关问题