我很难为使用旧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应该只有日期和前三个元素的永久链接后剥离破折号。
我的解决方案是在结合了这里的答案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>
我认为,用破折号代替下划线是一种代价昂贵的方法。但这至少在我的测试环境中是可行的。第一条规则逐个替换破折号。第二条规则然后从请求的URL中删除前缀。
RewriteBase /
# replace underscores with dashes
RewriteRule ^(news/index.php/.+?)_(.*) $1-$2 [L]
# strip "news/index.php"
RewriteRule ^news/index.php/(.*) /$1 [R=302,L]
我用你原来的方法玩了一点,使用N|next
标志,我的服务器也崩溃了。查看error.log
,似乎这个无限循环是由Apache通过添加"路径信息后缀"创建的,它用原始URL扩大了URL。所以它不停地用破折号替换下划线
您可以使用另一个标志DPI|discardpath
来阻止这个路径信息后缀,它给出了以下规则
RewriteRule ^(news/index.php/.+?)_(.*) $1-$2 [N,DPI]
这似乎也有效。虽然我必须承认,我真的不理解这个"路径信息后缀"的东西。在Apache的Bugzilla中也有一个条目,Bug 38642: mod_rewrite在替换发生后添加路径信息后缀
永远不要在启用301
的情况下进行测试,有关详细信息,请参阅此回答调试.htaccess重写规则的提示