2

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?

1 Answer 1

1

To handle category posts of the form /name-of-category/2008/10/20/sample-post then include something like the following directive after the existing redirect.

RewriteRule ^([a-z-]+)/\d{4}/\d\d/\d\d/(.+) /$1/$2 [R=301,L]

This would redirect /name-of-category/2008/10/20/sample-post to /name-of-category/sample-post. Note that the category consists of just the lowercase a-z and the - (hyphen), as in your example.

If you needed to allow more characters in the category then changing [a-z-] to [\w-] in the above RewriteRule pattern would allow a-z, A-Z, 0-9, _ and -. But it is better to be as restrictive as possible.

So, in summary:

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

:
4
  • I tried it..the 404 goes away. So thanks for that. But what's happening now is example.com/name-of-category/2008/10/20/sample-post gets redirected to example.com/name-of-category/sample-post which gets redirected to example.com/sample-post.
    – CuriousDev
    Commented Mar 9, 2017 at 17:09
  • What do your category URLs look like within WordPress? The /name-of-category/sample-post to /sample-post redirect would seem to be triggered by WordPress itself?
    – MrWhite
    Commented Mar 9, 2017 at 17:13
  • I don't seem to find anything that suggests that. When I open a category, there is no redirect. It just opens as-is. However when I open a post within a category like example.com/name-of-category/2008/10/20/sample-post, the redirection occurs and both the category and date are lost. i want to retain the category.
    – CuriousDev
    Commented Mar 10, 2017 at 3:18
  • To clarify, when you open a page within a category on your site, then URL is of the form: /name-of-category/sample-post? Try (temporarily) changing the status code of these two redirects to 307 (ie. R=307). Clear your browser cache and view the network traffic in the browser dev tools. What redirects/status do you see when requesting a URL of the form /name-of-category/2008/10/20/sample-post?
    – MrWhite
    Commented Mar 10, 2017 at 7:37

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.