.htaccess RedirectMatch Syntax

I am having a heck of a time trying to get the syntax correct on a 301 redirect. I’ve researched online quite a bit but as I am completely new to this it just isn’t clicking for me. I hope someone here can help.

I’m typing to write a rule that will change all links using the following format:

Read More

http://www.example.com/yyyy/mm/dd/page-name

To

http://www.example.com/category/page-name

In the above example, “/yyyy/mm/dd/” are variable for any number of dates. It’s a WordPress migration and the new theme does not allow us to preserve the day and name format for permalinks, we are trying to preserve several years’ worth of SEO “link juice.”

For various reasons I would prefer to do this using a 301 Redirect instead of as a Rewrite. Thanks in advance!

EDIT to add: Here is my attempt at it, I think I have mixed syntax for rewrites and redirects (among other things):

# 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

RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$ /category/$4/

Related posts

Leave a Reply

2 comments

  1. RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$ /category/$4 [L,R=301,NC]
    
  2. I appear to have gotten it working with this:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RedirectMatch 301 ^/[0-9]{4}/[0-9]{2}/[0-9]{2}/(.*)$ http://www.example.com/category/$1/
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress
    

    Thanks to everyone who replied, it helped me narrow things down at least.