Redirect category to url with /category

Your code is working correctly but the logic of the code is wrong. You are doing this: if we are in “blog” category, redirect to “blog” category. So, if you go to blog category you will be redirected forever. You should think carefully about what /blog url is and make the proper checking.

Anyway, when dealing with 301 redirections, I prefer to try .htaccess first. If it can be done through .htaccess, do it; you will save server resources because the redirection occurs before any PHP code is loaded.

Try this in your .htaccess:

RedirectMatch 301 ^/blog(.*)$ http://example.com/category/blog$1

The above code does this: if the url start with /blog(any-string-here), redirect to /category/blog(any-string-here) with 301 HTTP status code.

If you want to catch exactly /blog to /category/blog:

RedirectMatch 301 ^/blog$ http://example.com/category/blog