1

Wordpress automatically generates category pages, for example: https://planthardware.com/category/microgreens/recommended-equipment/

I want to redirect this to a custom page, for example: https://planthardware.com/microgreens-equipment/

What's the simplest way to do this?

I tried this (I suspect this is overcomplicated):

RewriteEngine on
RewriteBase /
RewriteRule ^category/microgreens/recommended-equipment/(.*) https://www.planthardware.com/microgreens-equipment/$1 [R=301,L]

And if I want to do multiple does something like the following make sense? Or is there redundancy and I don't need to turn RewriteEngine on for each redirect?

RewriteEngine on
RewriteBase /
RewriteRule ^category/microgreens/recommended-equipment/(.*) https://www.planthardware.com/microgreens-equipment/$1 [R=301,L]

RewriteEngine on
RewriteBase /
RewriteRule ^category/microgreens/growing/(.*) https://www.planthardware.com/microgreens-growing/$1 [R=301,L]

etc...

This code is currently not working on my site. I've cleared my wordpress cache, and I'm running Ezoic, I've also cleared my Ezoic cache.

I've also tried putting the following in .htaccess and it doesn't work:

RedirectPermanent https://www.planthardware.com/category/microgreens/recommended-equipment/ https://www.planthardware.com/microgreens-equipment/

Thanks!

19
  • Where in your .htaccess file are you adding these directives? You appear to be trying to match something at the end of the URL (and passing this through to the target) - but this is not stated in your example?
    – MrWhite
    Commented Oct 13, 2021 at 16:55
  • My htaccess was empty before attempting this so these are the first and only lines. Website was never redirected from another domain or anything like that in the past. I'm not trying to do anything fancy, just one url to another url.
    – Turillian
    Commented Oct 13, 2021 at 17:00
  • "My htaccess was empty" - Where are the WordPress directives? Are you using Apache?
    – MrWhite
    Commented Oct 13, 2021 at 17:11
  • I'm using Nginx I think. I don't know what a Wordpress directive is. The only lines in the .htaccess file are the lines above. It was empty (0 Bytes) before I tried to add redirects.
    – Turillian
    Commented Oct 13, 2021 at 17:17
  • 1
    Oh bingo I think you just solved it. There's another .htaccess file in public_htm and it's not empty. It has some wordpress redirects.
    – Turillian
    Commented Oct 13, 2021 at 18:16

1 Answer 1

0

Once you have found the right .htaccess file in the document root (ie. inside the public_html directory) then these redirect directives need to go at the top of the .htaccess file, before the existing WordPress directives. ie. Before the # BEGIN WordPress section.

You do not need to repeat the RewriteEngine or RewriteBase directives. (The RewriteBase directive is not actually being used here anyway.) The RewriteEngine On directive appears later in the file (inside the WordPress block), you do not need to repeat this.

To redirect as required you just need the following:

RewriteRule ^category/microgreens/recommended-equipment/ https://www.planthardware.com/microgreens-equipment/ [R=301,L]

You do not need the capturing subpattern (.*) and corresponding $1 backreference to redirect the example URL as stated.

NB: Test with 302 (temporary) redirect first to avoid potential caching issues.

If you are redirecting to the same scheme and hostname, you do not need to repeat these in the target URL. For example:

RewriteRule ^category/microgreens/recommended-equipment/ /microgreens-equipment/ [R=301,L]

You can use capturing subpatterns and backreferences to avoid repetition. For example:

RewriteRule ^category/(microgreens)/recommended-(equipment)/ /$1-$2/ [R=301,L]

The $1 backreference contains "microgreens" from the first capturing group (parenthesised subpattern) and $2 contains "equipment" from the second.


UPDATE: And for the second redirect you could write it like this (using backreferences):

RewriteRule ^category/(microgreens)/(growing)/ /$1-$2/ [R=301,L]

To redirect to /microgreens-growing/.


Aside:

I've also tried putting the following in .htaccess and it doesn't work:

RedirectPermanent https://www.planthardware.com/category/microgreens/recommended-equipment/ https://www.planthardware.com/microgreens-equipment/

Note that the RedirectPermanent directive belongs to mod_alias (as opposed to mod_rewrite RewriteRule), consequently it runs much later, after all the other mod_rewrite directives, despite the apparent order of the directives in the config file. For this reason, it is advisable not to mix redirects from both modules, since you can get unexpected conflicts.

Note that the RedirectPermanent directive matches against the root relative URL-path (starting with a slash), not the absolute URL. So, the above directive wouldn't match anyway. It should be written like this instead:

RedirectPermanent /category/microgreens/recommended-equipment/ https://www.planthardware.com/microgreens-equipment/

OR (omitting the scheme + hostname on the target URL):

RedirectPermanent /category/microgreens/recommended-equipment/ /microgreens-equipment/
3
  • 1
    Thanks MrWhite.
    – Turillian
    Commented Oct 13, 2021 at 18:26
  • @Turillian You're welcome. I've updated my answer with a bit more information.
    – MrWhite
    Commented Oct 13, 2021 at 18:37
  • Now if only Bluehost tools didn't just go down so I could implement this... heh
    – Turillian
    Commented Oct 13, 2021 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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