Forum Moderators: phranque

Message Too Old, No Replies

.htaccess redirect exact url match

         

lonewolfonline

6:11 pm on Apr 4, 2008 (gmt 0)

10+ Year Member



Hello, me again,

I was wondering if somebody could help me, I have a Wordpress install that puts a base "tag" in the url for each category, but the base tag by itself does not exist. I would like to redirect the non-existent page to a page that does exist. Examples are probably clearer.

Here are urls to two categories

http://www.example.net/category/this-is-my-cat-1/
http://www.example.net/category/this-also-is-my-cat-2/

However if somebody removes the this-is-my-cat-1 bit from the url and just tries to go to http://www.example.net/category/ they get a 404.

How do I redirect http://www.example.net/category/ to http://www.example.net/a-static-page/ while keeping all the original links intact.

The redirects I have tried redirect /category/ as well as anything after, breaking the category links.

Thanks in advance

Regards
Tim

jdMorgan

10:30 pm on Apr 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> The redirects I have tried redirect /category/ as well as anything after, breaking the category links.

Please post your code as a basis for discussion.

Thanks,
Jim

lonewolfonline

8:41 am on Apr 5, 2008 (gmt 0)

10+ Year Member



Well, the first rule I tried was a simple redirect:

RedirectPermanent /category/ http://www.example.net/my-static-page/

Which matched everything with "/category/" in the url.

Next I tried a redirectmatch

RedirectMatch 301 /category/? http://www.example.net/my-static-page/

And then I tried a few variations on rewrite rules

RewriteRule ^/category/?$ http://www.example.net/my-static-page/

Then I tried something overly complex involving a not match on a-zA-z0-9-_ for anything after /category/, but that mainly ended up as 500 errors!

I found a wordpress plugin which would completely remove the /category/ from all URLs, but I would need a single rewrite rule to then redirect every url containing /category/ to the non-category.

http://www.example.net/category/this-is-my-cat-1/ => http://www.example.net/this-is-my-cat-1/

Would that just be one rule? easier? I guess its better for SEO as the category keywords are closer to the domain.

Thanks for your help

Regards
Tim

jdMorgan

2:38 pm on Apr 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to anchor your pattern used in the RedirectMatch directive. RedirectMatch uses regular-expressions pattern-matching -- much more powerful than the simple prefix-matching used by the Redirect directive:

RedirectMatch 301 ^/category/?$ http://www.example.com/my-static-page/

See the resources cited in our forum charter and the threads in our library for more info.

Jim

lonewolfonline

8:13 pm on Apr 7, 2008 (gmt 0)

10+ Year Member



Hey Jim, Thanks I have it working now the pattern is anchored!

Thanks again for your help

Regards
Tim