title: htaccess rewrite rule with regular expression
body: <p>I have a hard time to create rewrite rule for a redirect using part of an old URL. Example:</p>
  
  <p>Old URL:
  <a href="http://www.example.com/news/index.php/2014/11/07/my-blog-post-from-old-site" rel="nofollow">http://www.example.com/news/index.php/2014/11/07/my-blog-post-from-old-site</a>
  or
  <a href="http://www.example.com/news/index.php/2014/11/07/my_blog_post_from_old_site" rel="nofollow">http://www.example.com/news/index.php/2014/11/07/my_blog_post_from_old_site</a></p>
  
  <p>New URL:
  <a href="http://www.example.com/2014/11/07/my-blog-post" rel="nofollow">http://www.example.com/2014/11/07/my-blog-post</a></p>
  
  <p>New URL should to have only dates and first three elements of a permalink after stripping from dashes. Even I'm not sure if can be done using .htaccess rule but for sure can be done using PHP.</p>
  
  <p>Here are my .httaccess rules</p>
  
  <pre><code>RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
  RewriteCond %{HTTP_HOST} ^www.example.com [NC]
  RewriteRule ^(/?news/index.php/.*/[^/]*?)_([^/]*?_[^/]*)$ $1-$2 [N]
  RewriteRule ^(/?news/index.php/.*/[^/]*?)_([^/_]*)$ $1-$2 [R=301]
  RewriteRule ^news/index.php/([^/]+)-([^/]+)-(.*)$ http://www.example.com/$1-$2-$3 [L,R=301,NC]
  </code></pre>