$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>.htaccess rule to redirect old URLs to new structure|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

.htaccess rule to redirect old URLs to new structure

matteradmin9PV0评论
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.

Post a comment

comment list (0)

  1. No comments so far