$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - .htaccess too many redirects based on category slug|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

categories - .htaccess too many redirects based on category slug

matteradmin9PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far