.htaccess rule to redirect old URLs to new structure

admin2025-06-05  1

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

The old URLs were in the form:


The new URLs are in the form:


I'm attempting to redirect any hits at
to 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?

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

The old URLs were in the form:

https://www.example/collection/item/item-name-123

The new URLs are in the form:

https://www.example/collection/item/123

I'm attempting to redirect any hits at
https://www.example/collection/item/item-name-123 to https://www.example/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?

Share Improve this question edited Dec 1, 2018 at 19:13 MrWhite 3,8911 gold badge20 silver badges23 bronze badges asked Dec 1, 2018 at 17:33 GaryGary 636 bronze badges 1
  • 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 Commented Dec 1, 2018 at 17:54
Add a comment  | 

1 Answer 1

Reset to default 1

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749135731a316662.html

最新回复(0)