.htaccess - htaccess rewrite rules with regexp and inifite redirect -
i have hard time create rewrite rule redirect using part of old url wp. 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 have dates , first 3 elements of permalink after stripping dashes.
my solution came after combining answers here https://stackoverflow.com/a/32852444/1090360 , here https://stackoverflow.com/a/1279758/1090360
somehow part replacing underscores dashes creates infinite redirect , server freezes. if remove part replacing underscores dashes rest works should.
here .httaccess rules
<ifmodule mod_rewrite.c> rewriteengine on rewritebase / #replace underscores dashes rewriterule ^(/news/.*/[^/]*?)_([^/]*?_[^/]*)$ $1-$2 [n] rewriterule ^(/news/.*/[^/]*?)_([^/_]*)$ $1-$2 [r=301,l,nc] #redirect 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>
i think, expensive way replace underscores dashes. works @ least in test environment. first rule replaces dashes 1 one. second rule removes prefix requested url.
rewritebase / # replace underscores dashes rewriterule ^(news/index.php/.+?)_(.*) $1-$2 [l] # strip "news/index.php" rewriterule ^news/index.php/(.*) /$1 [r=302,l] i played bit more original approach using n|next flag , crashed server too. looking error.log, seems infinite loop created apache adding "path info postfix", enlarges url original url. , keeps replacing underscores dashes on , on.
you can prevent path info postfix flag dpi|discardpath, gives following rule
rewriterule ^(news/index.php/.+?)_(.*) $1-$2 [n,dpi] this seems work too. although must admit, don't understand "path info postfix" thing. there's entry in apache's bugzilla, bug 38642: mod_rewrite adds path info postfix after substitution occured
never test 301 enabled, see answer tips debugging .htaccess rewrite rules details.
Comments
Post a Comment