Welcome to WebmasterWorld Guest from 54.221.9.209

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

htaccess redirect works partially

     
4:01 am on May 28, 2013 (gmt 0)

New User

5+ Year Member

joined:May 27, 2008
posts:39
votes: 0


I am running a Wordpress based website. Lucy from WebmasterWorld helped me written the following htaccess redirect.

RewriteRule ^archives/(category|author|tag)/((?:[a-z]{5,}/|feed/)+)(page/[0-9]+/)?(?:[a-z][a-z](?:_MX|_BR|_TW|-CN)?/)? http://www.example.com/blog/$1/$2$3 [R=301,L]


The above should automatically strip off the /archives/ for the 2 example URLs below. I've tested it and it works.
http://www.example.com/blog/archives/tag/delete/
http://www.example.com/blog/archives/tag/recover/

Weirdly, I can't figure out why the htaccess is not stripping off the /archives/ for the URL below which is actually the same as the 2 URLs above.
http://www.example.com/blog/archives/tag/text/

I thought this is caused by Wordpress trying to guess the correct URL. Added the code below to functions.php but didn't solve the problem.

add_filter('redirect_canonical', 'stop_guessing');
function stop_guessing($url) {
if (is_404()) {
return false;
}
return $url;
}


Any advice would be much appreciated.

[edited by: phranque at 4:32 am (utc) on May 28, 2013]
[edit reason] please use example.com [/edit]

7:08 am on May 28, 2013 (gmt 0)

Junior Member

joined:Apr 6, 2013
posts:149
votes: 0


At a glance, the reason seems to be because the pattern requires the word after "/tag/" be at least 5 letters, but "text" is only 4 letters. As for *why* the pattern requires at least 5 letters... well... that's a question only you and Lucy can answer. Personally, I think the whole pattern overly specific and unnecessarily complicated.
9:24 am on May 28, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


The pattern is likely very specific such that it doesn't accidentally match other URL requests that it should not match.

This is one of the many cases where the oft recommended ^(.*)/(.*)/(.*)(.*)?((.*)_(.*))? etc, would wreak havoc.

That said, the {5,} requirement is a bit odd. You'll need to see what requests, if any, with length 1, 2, 3 or 4 this part must never match before altering the pattern.
6:41 pm on May 28, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

joined:Apr 9, 2011
posts:12707
votes: 244


The pattern is likely very specific such that it doesn't accidentally match other URL requests that it should not match.

Yes, there were clear and specific reasons why the RewriteRules had to be worded exactly that way. The quoted line is only one piece of a much longer htaccess.

There are also clear and specific reasons why I will not provide any further answers in this thread :(