Home > Software engineering >  Ignore URL segments in 301 Redirect rule
Ignore URL segments in 301 Redirect rule

Time:02-15

Struggling with a 301 redirect rule to ignore additional URL segments. Here is the goal:

Redirect URLs in this format: example.com/product-category/category-parent/category-child/ to a new target URL.

Here is the rule in .htaccess:

Redirect 301 /product-category/category-parent/category-child/ https://newurl.com/

The redirecting is happening fine, except that the child segment of the URL is passed thru to the new URL so I end up with newurl/category-child/ which is a 404.

EDIT: Tried using RedirectMatch for the few specific nested category URLs with no luck:

RedirectMatch 301 /product-category/parent/child/ https://newurl.com/

Still produces https://newurl.com/child which is a 404.

CodePudding user response:

Sorted out how to do what I needed. Apache's RedirectMatch is what I needed with the proper regular expression.

RedirectMatch permanent "^/product-category/(.*)" "https://newurl.com/"

This will match any number of any character after the /product-category/ segment and redirect to the new target URL, which is exactly what I needed.

  • Related