Support » Plugins » Hacks » Help with WordPress Permalink 301 Redirect via .htaccess

  • Resolved brometheus55


    I am currently attempting to change the permastructure of all of my WordPress posts from:
    to just

    I have already made the change in WordPress and added the following code to my htaccess file to initiate the 301 redirects:

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

    Now everything is working perfectly except for 1 thing. It has caused all archived date pages, for example, to redirect to, which causes 404s.

    My question is how do I edit the code to not reflect those archived dates. I imagine it’s something fairly simple but i know minimal coding and have already spent hours searching online only to come up short. I’m very surprised as the code I used was given as a common fix. I assume most people didn’t either use their calendar widgets, or never noticed this problem who used this method.

    Anyway please and thanks for help.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator bcworkz


    Something like this might work:
    RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/([^0-9]{2})(.+)$$3$4

    The caret ‘^’ inside square brackets means do not match the listed characters. It works for your example but it may not work for all situations. To capture all situations, you may need to use a rewrite rule block listing multiple conditions.

    Yes that works! Thank you so much for your help!

    I actually noticed a few other problems that still exist pertaining to the archives again. Here is what’s going on:

    On a single day or a single month page, clicking the next page feature to get to page 2 breaks. Examples:

    Page 2 on monthly archives: (ie: Jun >> June page 2) redirects>>>>>>>>>

    Page 2 on a single day of archives: redirects>>>>>>> 404 not found

    Is this something you can help me with? Let me know if you need to to outline the problem better. Thanks.

    Moderator bcworkz


    I was afraid it would not be that easy. I think you are better off using the mod_rewrite module instead of trying to create the ideal regexp for a redirect. With mod_rewrite you can stack multiple conditions linked by logical AND & OR to determine if a particular rewrite rule should be applied. You can see an example of this because WP uses one to enable permalinks. It’s the portion after # BEGIN WordPress in your installation’s .htaccess file.

    Unfortunately, I can’t offer any code to get you going, one silly error and your entire site returns 500 errors. I don’t want to be responsible for that. There’s lots of examples on the ‘net and there’s the official documentation. I suggest you start with a single basic condition and rule. Incrementally build on that. You will have a better idea where things aren’t right this way. The error logs often give a good clue as well.

    Sorry I can’t be of more help. If you get stuck on something, I may be able to shed some light, but I’m uncomfortable producing the entire block of code. I hope you understand. Good luck.

    Thanks so much for the response. Doing some more searching on it I was able to find someone with a very similar problem here:

    The recommended fix was to add:

    RewriteCond %{REQUEST_URI} !page
    RewriteRule ^[0-9]{4}\/+[0-9]{2}\/+(.+)$ /blog/$1 [L,R=301]

    So i took that and basically replaced it with the previous code I was using. (ended up being different slightly than yours fyi, but yours works the same way interchangeably):

    Original code:

    RedirectMatch 301 ^/[0-9]{4}/[0-9]{2}/([a-z0-9\-/]{4,})$1

    New code (seems to fix archives problem):

    RewriteCond %{REQUEST_URI} !page
    RewriteRule ^[0-9]{4}\/+[0-9]{2}\/([a-z0-9\-/]{4,})$1  [L,R=301]

    Now my question for you, sir, is how does the code look and how does it look compared to the example fix from that page? Did I transpose everything correcty? Am I missing any characters or commands? Although it seems everything is working fine now, Just would like to doublecheck with someone who actually know what they are doing to see if you think this code holds water.

    Thanks you very, very much for your help again.

    btw, I have no idea what (.+)$ means

    Moderator bcworkz


    I can’t comment on the regexp itself as I’m unsure of all the possible URL variants it needs to match or not match, but it does appear to be a valid regexp. Your site would probably be broken if it were not.

    As for the rewrite rule in general, perhaps you just didn’t include it here, but you should include the entire context, similar to what WP does for the permalinks. Based on what you posted, the entire block might look like this:

    <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteBase /
      RewriteCond %{REQUEST_URI} !page
      RewriteRule ^[0-9]{4}\/+[0-9]{2}\/([a-z0-9\-/]{4,})$1  [L,R=301]

    The RewriteBase may need to be altered if your WP installation is not in the root of your site. The nice thing about this approach is it’s easy to add more conditions if you discover more anomalies.

    As for what (.+)$ means, it matches anything 1 or more characters in length that occur at the end of the URL not matched by prior regexp. Whatever is matched is assigned to a $# back reference.


    That was just what I was looking for was someone to confirm the validity of the regexp. Good to know thanks!

    Also, yes, I just didn’t include the whole code snippet. I did place it in between the <ifModule> tags after RewriteBase /.

    Thanks for the additional information and for answering my question as (.+)$ means. Although not sure I fully understand. I know when I was testing if I added that to the end of the current line I’m using, it would seemingly work in the same way.

    Thanks again for your help and quick responses, you have been extremely helpful!

    Moderator bcworkz


    I’m pleased to be of service and that you arrived at a workable solution.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Help with WordPress Permalink 301 Redirect via .htaccess’ is closed to new replies.