I have a hard time to create rewrite rule for a redirect using part of an old URL for WP. Example:

Old URL:




New URL:


New URL should to have only dates and first three elements of a permalink after stripping from dashes. If I will remove part with replacing underscores to dashes all the rest works as should.

Here are my .httaccess rules

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

#replace underscores with dashes
RewriteRule ^(/news/.*/[^/]*?)_([^/]*?_[^/]*)$ $1-$2 [N]
RewriteRule ^(/news/.*/[^/]*?)_([^/_]*)$ $1-$2 [R=301]

#redirect to new URL
RewriteRule ^news/index\.php/([^-]+-[^-]+-[^-]+).* /$1 [R=301,L,NC]

#WP standard stuff
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

1 Answer 1


Found that solution useful

I think, this is an expensive way to replace underscores with dashes. But this works at least in my test environment. The first rule replaces dashes one by one. The second rule then removes the prefix from the requested URL.

RewriteBase /

# replace underscores with dashes 
RewriteRule (.+?)_(.+) $1-$2 [R=302,L]

# strip "news/index.php"
RewriteRule ^news/index.php/(.*) /$1 [R=302,L]

Your Answer

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

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