Skip to main content
2 of 4
Formatting. Exemplified domain name according to meta. (Please use "example.com")
MrWhite
  • 43.1k
  • 4
  • 50
  • 90

.htaccess and permanent redirection with a special condition

I have a WordPress website hosted on Bluehost which contains the following URL pattern:

http://www.example.com/2017/01/30/sample-post/

I wanted to permanent redirect it to use this:

http://www.example.com/sample-post/

So I opened .htaccess kept in the example.com folder and changed it to the following with the help of a suggestion by w3dk.

RewriteEngine On
RewriteRule ^\d{4}/\d\d/\d\d/(.+) /$1 [R=301,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

After I wrote this rewrite rule, everything of the URL format example.com/2017/01/30/sample-post works great. However, there are a few URLs in the website that have the format example.com/name-of-category/2008/10/20/sample-post and are throwing a 404 as the result of the above rewrite rule.

I want to permanently redirect www.example.com/name-of-category/2008/10/20/sample-post to www.example.com/name-of-category/sample-post.

What change do I need to make in my .htaccess file?