htaccess 使用正则表达式重写规则
htaccess rewrite rule with regular expression
我很难使用旧 URL 的一部分为重定向创建重写规则。示例:
旧URL:
http://www.example.com/news/index.php/2014/11/07/my-blog-post-from-old-site
要么
http://www.example.com/news/index.php/2014/11/07/my_blog_post_from_old_site
新URL:
http://www.example.com/2014/11/07/my-blog-post
新的 URL 在从破折号中剥离后应该只有日期和永久链接的前三个元素。即使我不确定是否可以使用 .htaccess 规则来完成,但肯定可以使用 PHP.
这是我的 .httaccess 规则
RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(/?news/index.php/.*/[^/]*?)_([^/]*?_[^/]*)$ - [N]
RewriteRule ^(/?news/index.php/.*/[^/]*?)_([^/_]*)$ - [R=301]
RewriteRule ^news/index.php/([^/]+)-([^/]+)-(.*)$ http://www.example.com/-- [L,R=301,NC]
只匹配块,直到遇到连字符 (-
):
RewriteRule ^news/index\.php/([^-]+-[^-]+-[^-]+).* / [R=301,L,NC]
我很难使用旧 URL 的一部分为重定向创建重写规则。示例:
旧URL: http://www.example.com/news/index.php/2014/11/07/my-blog-post-from-old-site 要么 http://www.example.com/news/index.php/2014/11/07/my_blog_post_from_old_site
新URL: http://www.example.com/2014/11/07/my-blog-post
新的 URL 在从破折号中剥离后应该只有日期和永久链接的前三个元素。即使我不确定是否可以使用 .htaccess 规则来完成,但肯定可以使用 PHP.
这是我的 .httaccess 规则
RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(/?news/index.php/.*/[^/]*?)_([^/]*?_[^/]*)$ - [N]
RewriteRule ^(/?news/index.php/.*/[^/]*?)_([^/_]*)$ - [R=301]
RewriteRule ^news/index.php/([^/]+)-([^/]+)-(.*)$ http://www.example.com/-- [L,R=301,NC]
只匹配块,直到遇到连字符 (-
):
RewriteRule ^news/index\.php/([^-]+-[^-]+-[^-]+).* / [R=301,L,NC]