1

I want to redirect all subcategories who belong to the category named symptoms to a same page.

I did this:

RewriteRule ^category/symptoms/(.*)$ https://my-site/com/list/$1 [L,R=301]

Wordpress redirects but always add the subcategory at the end. For example: my-site/com/category/symptoms/fever is redirected to my-site/com/list/fever.

How to stop it adding the subcategory?

  • What is the URL you want to redirect to? – MrWhite Oct 3 '18 at 10:34
1

The $1 is grabbing the value from (.*)$, so to remove the subcategory, you'll need to remove the flag

RewriteRule ^category/symptoms/(.*)$ https://my-site/com/list/ [L,R=301]

  • Then there's no reason to have the (.*)$ in the RewriteRule pattern to begin with. – MrWhite Oct 3 '18 at 11:23
1

If I get your question right you want to redirect all https://my-site.com/category/symptoms/[subcategory] to https://my-site.com/list/, right?

Just do:

RewriteRule ^category/symptoms/.*$ https://my-site/com/list/ [L,R=301]

This replaces everything from category/symptoms/ until the end of the line (marked by $).

Your symptoms/(.*)$ saves everything between pattern/ and the end of the line and substitutes it for $1 in your replacement string.

See this tutorial for references in replacement strings in regular expressions: https://www.regular-expressions.info/replacebackref.html

  • If you want to tidy up the regex some more then the trailing .*$ on the RewriteRule pattern is superfluous. – MrWhite Oct 3 '18 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.