Change of permalink structure – redirects in htaccess breaks the archive links

When I started with my blog, the permalinks and following structure:

http://<domain>/%year%/%monthnum%/%day%/%postname%/

Some time ago, I changed the permalink structure to “http://<domain>/%postname%/” only.

Read More

In order to tackle the external links to the old permalink structure, I added the following line in my htaccess-file:

RedirectMatch 301 /([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ http://<domain>/$4

Now the problem is that the redirect line breaks the archive links, eg. “http://<domain>/2010/09/02” is redirected to the front page instead of showing the posts from Sept 2, 2010.

Is it possible to correct the redirect directive in htaccess, or am I not going to be able to eat the cake and have it too?

Related posts

Leave a Reply

1 comment

  1. RedirectMatch of Mod_Alias is not as powerful as Mod_Rewrite. If you have Mod_Rewrite on your host (Pretty Permalinks make use of it for example) you could make the redirect only if the URL is not in the Archive link format.

    To test for that case, there is RewriteCond and to make the redirect command, there is the RewriteRule ... [R=301] directive.

    An Example based on your data:

    RewriteCond %{REQUEST_URI} !^/((20|19)[0-9]{2})/([0-9]{2})/[0-9]{2}$ [NC]
    RewriteRule ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$ http://<domain>/$4 [R=301,L]
    

    This is untested but I think it should do the work. I’ve used quantifier ({2}) to better specifiy how many numbers you expect. Next to that in the RewriteCond I’ve created a pattern that only matches the 20.. and 19.. years.

    The first line (the condition, RewriteCond) checks not to match an Archive-URL and only if not matched, the rule to do the redirect will be executed. The RewriteRule is basically doing the same as your RedirectMatch directive.