0

What's the best way to rewrite URLs as 301 redirects with the following conditions?

Sample old URLs to rewrite:
/c/garments-apparel/red-yellow-polka-dress-10_450
/c/shoes-and-accessories/black-suede-boots-02_901

Conditions:

  1. Change c to category
  2. Remove trailing number (including connecting dash) from URL (example: -10_450 and -02_901)

New URLs should be:
/category/garments-apparel/red-yellow-polka-dress
/category/shoes-and-accessories/black-suede-boots

Note that changes will be applied to an .htaccess file on a Wordpress environment.

2 Answers 2

1

You can have this rule just below RewriteEngine On line:

RewriteEngine On

RewriteRule ^c/([\w-]+/.+)-[\d_]+/?$ /category/$1 [L,NC,R=301]
Sign up to request clarification or add additional context in comments.

3 Comments

This makes it lose the category name of the URL eg: shoes-and-accessories or garments-apparel.
That must be due to bad cache in browser. On my apache this redirects http://localhost/c/garments-apparel/red-yellow-polka-dress-10_450 to http://localhost/category/garments-apparel/red-yellow-polka-dress
You're right! I refreshed my permalinks & it's redirecting correctly now. Thank you!
1

you can use the regex

[-_]\d+

to replace the trailing numbers with "" (empty string) demo

then use the regex

\/c\/

and replace with /category/ demo

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.