0

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

Old URL:

http://www.example.com/news/index.php/2014/11/07/my-blog-post-from-old-site

or

http://www.example.com/news/index.php/2014/11/07/my_blog_post_from_old_site

New URL:

http://www.example.com/2014/11/07/my-blog-post

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]
</IfModule>
0

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, privacy policy and cookie policy

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