The old URLs were in the form:


The new URLs are in the form:


I'm attempting to redirect any hits at
https://www.example.com/collection/item/item-name-123 to https://www.example.com/collection/item/123 but I can't seem to get the .htaccess rule down.

What I've tried is:

RewriteRule ^(.*)/item/(.*)-([0-9]+)$ $1/item/$3 [R=301]

Which doesn't appear to be working. Am I missing something simple?

  • It's not clear from your example which parts of the URL are variable and which are fixed and what characters are permitted in the different sections. Your regex could perhaps be optimised. – MrWhite Dec 1 '18 at 17:54

Am I missing something simple?

You need the L flag (to prevent further processing) and this directive should go at the very top of your existing .htaccess file (ie. before the # BEGIN WordPress section).

Unless you have already defined an appropriate RewriteBase directive (the WordPress front-controller usually has this), then you also need to prefix the substitution string with a slash (although it is recommended to do this anyway for external redirects).

Otherwise, your regex and substitution string should work OK.

In other words, at the top of your file:

RewriteRule ^(.*)/item/(.*)-([0-9]+)$ /$1/item/$3 [R=302,L]

Test with 302 (temporary) redirects (to avoid caching issues) and change to a 301 only when you have confirmed it works as intended.

You will need to clear your browser cache before testing.

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