【发布时间】:2015-09-30 15:55:14
【问题描述】:
我很难使用 WP 的旧 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
新网址:
http://www.example.com/2014/11/07/my-blog-post
从破折号中删除后,新 URL 应该只有日期和永久链接的前三个元素。
我的解决方案是在将https://stackoverflow.com/a/32852444/1090360 和https://stackoverflow.com/a/1279758/1090360 的答案结合起来后得出的
不知何故,用破折号替换下划线的部分会创建无限重定向并且服务器会冻结。如果我将通过将下划线替换为破折号来删除部分,那么其余部分都应该正常工作。
这是我的 .httaccess 规则
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#replace underscores with dashes
RewriteRule ^(/news/.*/[^/]*?)_([^/]*?_[^/]*)$ $1-$2 [N]
RewriteRule ^(/news/.*/[^/]*?)_([^/_]*)$ $1-$2 [R=301,L,NC]
#redirect to new URL
RewriteRule ^news/index\.php/([^-]+-[^-]+-[^-]+).* /$1 [R=301,L,NC]
#WP standard stuff
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
【问题讨论】:
标签: .htaccess mod-rewrite url-rewriting rewrite url-redirection