htaccess rewrite rule with regular expression

混江龙づ霸主 提交于 2019-12-04 05:15:06

问题


I have a hard time to create rewrite rule for a redirect using part of an old URL. Example:

Old URL: http://www.example.com/news/index.php/2014/11/07/my-blog-post-from-old-site or http://www.example.com/news/index.php/2014/11/07/my_blog_post_from_old_site

New URL: http://www.example.com/2014/11/07/my-blog-post

New URL should to have only dates and first three elements of a permalink after stripping from dashes. Even I'm not sure if can be done using .htaccess rule but for sure can be done using PHP.

Here are my .httaccess rules

RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(/?news/index.php/.*/[^/]*?)_([^/]*?_[^/]*)$ $1-$2 [N]
RewriteRule ^(/?news/index.php/.*/[^/]*?)_([^/_]*)$ $1-$2 [R=301]
RewriteRule ^news/index.php/([^/]+)-([^/]+)-(.*)$ http://www.example.com/$1-$2-$3 [L,R=301,NC]

回答1:


Just match for the blocks until hyphen (-) is encountered:

RewriteRule ^news/index\.php/([^-]+-[^-]+-[^-]+).* /$1 [R=301,L,NC]


来源:https://stackoverflow.com/questions/32852356/htaccess-rewrite-rule-with-regular-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!