WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Help with WordPress Permalink 301 Redirect via .htaccess (10 posts)

  1. brometheus55
    Member
    Posted 1 year ago #

    I am currently attempting to change the permastructure of all of my WordPress posts from:
    /%year%/%monthnum%/%postname%/
    to just
    /%postname%/.

    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})/(.+)$ http://www.mydomain.com/$3

    Now everything is working perfectly except for 1 thing. It has caused all archived date pages, for example http://www.mydomain.com/2013/8/20, to redirect to http://www.mydomain.com/20, 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.

  2. bcworkz
    Member
    Posted 1 year ago #

    Something like this might work:
    RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/([^0-9]{2})(.+)$ http://www.mydomain.com/$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.

  3. brometheus55
    Member
    Posted 1 year ago #

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

  4. brometheus55
    Member
    Posted 12 months ago #

    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)
    http://www.mysite.com/2013/07/page/2/ redirects>>>>>>>>> http://www.mysite.com/page/2/

    Page 2 on a single day of archives:
    http://www.mysite.com/2013/08/21/page/2/ redirects>>>>>>> http://www.mysite.com/21/page/2/ 404 not found

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

  5. bcworkz
    Member
    Posted 12 months ago #

    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.

  6. brometheus55
    Member
    Posted 11 months ago #

    Thanks so much for the response. Doing some more searching on it I was able to find someone with a very similar problem here: http://wpquestions.com/question/showChrono/id/8468

    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,}) http://www.sweetfreestuff.com/$1

    New code (seems to fix archives problem):

    RewriteCond %{REQUEST_URI} !page
    
    RewriteRule ^[0-9]{4}\/+[0-9]{2}\/([a-z0-9\-/]{4,}) http://www.MYSITE.com/$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.

  7. brometheus55
    Member
    Posted 11 months ago #

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

  8. bcworkz
    Member
    Posted 11 months ago #

    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,}) http://www.MYSITE.com/$1  [L,R=301]
    </IfModule>

    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.

    `

  9. brometheus55
    Member
    Posted 11 months ago #

    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!

  10. bcworkz
    Member
    Posted 11 months ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic