categories - .htaccess too many redirects based on category slug

admin2025-06-03  3

I moved my wordpress page up a directory and also changed a bunch of categories with new names. But when I try to set a 301 redirect via .htaccess and then access that category url, the browser errors with a "too many redirects" message.

Redirect 301 /category/cat-a/subcat-a/subsubcat-a/ /

Redirect 301 /category/cat-b/subcat-b/ /

... bunch more categories

the "category" slug stays the same in the new and old names.

I know there is a flag that makes a rule "final" but I'm not sure how I could make. Cause I also tried setting the flag afterwards like:

Redirect 301 /category/cat-a/subcat-a/subsubcat-a/ / [L]

I've tried this and many other variations but neither did work out in my favour.

Do I miss something. Im also not sure if there is a regex that solves that problem. Basically from what i've understand, this creates an infinite loop.

I moved my wordpress page up a directory and also changed a bunch of categories with new names. But when I try to set a 301 redirect via .htaccess and then access that category url, the browser errors with a "too many redirects" message.

Redirect 301 /category/cat-a/subcat-a/subsubcat-a/ https://example/category/newcat-a/newsubcat-a/

Redirect 301 /category/cat-b/subcat-b/ https://example/category/newcat-b/mynewsubcat-b/

... bunch more categories

the "category" slug stays the same in the new and old names.

I know there is a flag that makes a rule "final" but I'm not sure how I could make. Cause I also tried setting the flag afterwards like:

Redirect 301 /category/cat-a/subcat-a/subsubcat-a/ https://example/category/newcat-a/newsubcat-a/ [L]

I've tried this and many other variations but neither did work out in my favour.

Do I miss something. Im also not sure if there is a regex that solves that problem. Basically from what i've understand, this creates an infinite loop.

Share Improve this question asked Feb 1, 2019 at 10:33 WilliWilli 691 silver badge7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

It's not clear from what you have posted why you are getting a redirect loop. We would need to see the rest of .htaccess file. However ....

You may be getting a conflict with existing mod_rewrite (ie. RewriteRule) directives. Redirect is a mod_alias directive.

You no doubt have existing mod_rewrite directives with the WordPress front-controller. You have other redirects also. You should avoid mixing redirects from both modules since they may not execute in the order you expect (mod_rewrite always runs before mod_alias, despite the apparent order in the config file).

I know there is a flag that makes a rule "final" but ...

That "flag" applies to mod_rewrite RewriteRule, not Redirect (mod_alias). By adding that flag to Redirect in this context it will simply be ignored.

You should change these mod_alias Redirect directives to mod_rewrite RewriteRule and include them at the very top of your file.

For example:

RewriteRule ^(category)/cat-a/subcat-a/subsubcat-a/(.*) /$1/newcat-a/newsubcat-a/$2 [R=302,L]
RewriteRule ^(category)/cat-b/subcat-b/(.*) /$1/newcat-b/mynewsubcat-b/$2 [R=302,L]

This assumes that everything after the categories are copied onto the end of the target URL. eg. .../subsubcat-a/foo/bar becomes .../newsubcat-a/foo/bar. (Which is what your original Redirect directives would have done.)

$1 is a backreference to "category" (simply saves repetition) and $2 is everything after the category string.

The L (last) flag is required here.

Test with 302 and only change to 301 when you are sure it's working OK - to avoid caching issues.

You will need to clear your browser cache before testing.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748954191a315121.html

最新回复(0)