• Resolved AdCentris

    (@vest24)


    Hello,

    I have a problem and I’m not sure why this is happening.

    I did a migration from an older shop to a wp/woocommerce shop. What I’m trying to do now is to redirect all the old links to the new ones.

    For that, I’m using htaccess 301 redirects. I have 500 lines of redirects. What is happenig now is that I have 404 result but only for redirects placed more at the end of the list. If I move the 404 problematic redirect at the top of the list, it’s working. If I move the top redirects to the end of the list, they are not working.

    I tried to place the redirect above the WP generated code and below. Same results.

    Tried also with rewrite rules, same behaviour.

    I tried to search for an answer but nothing found.
    Any ideas? Thank you!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello vest24,

    Hope you are having great week so far!

    I think the problem is that you are having way too much redirects in your .htaccess

    Please check this page from the Apache project – https://httpd.apache.org/docs/2.4/configuring.html

    You will find there this explanation:

    The maximum length of a line in normal configuration files, after variable substitution and joining any continued lines, is approximately 16 MiB. In .htaccess files, the maximum length is 8190 characters.

    So it seems like you can’t have .htaccess files with more than 8190 characters. If you divide that by the number of your lines, you should have around 16 character on line. ??

    Maybe you could use some regex for some of the links?
    Or they are all different and that is not possible?

    Another workaround you could use is to put this redirects in php file, and then redirect from there.

    You will need to check which is the requested url and redirect accordingly to the new one

    You can check the requested URL with this php code:

    $actual_link = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

    And then use something like this:

    <?php
    $actual_link = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    
    if ($actual_link == "https://domain.com/some-link";){
        header('HTTP/1.1 301 Moved Permanently');
        header('Location: https://www.domain.com/another-link');
    }
    ?>

    Please note I haven’t tested this code, and redirect may result in redirect loops and leave out from your WordPress install unable to log in. So use with caution.

    But that will require hell of a lot if statements, and will put a huge load on the server. So I am not really sure how to handle this with so many redirects.

    Maybe you could separate this in a few php files in a different directories?

    Hope this helps.

    Thanks,
    Ivan

    Thread Starter AdCentris

    (@vest24)

    That is a new information about the lenght in htaccess indeed. I wanted to do the php approach but I thought to try some plugins first. I tried several plugins for redirect, but I had some issues also with them. The one that seems to work it’s called Redirect and imported redirects straight from htaccess.

    So problably my way to do redirects in wordpress was not the best.

    Thank you for support!

    Hi vest24,

    That is a new information about the lenght in htaccess indeed.

    Actually for me too, never actually have the need of very long .htaccess files. ??

    The one that seems to work it’s called Redirect and imported redirects straight from htaccess.

    Glad you have found plugin that suits you. This should be much more better than writing 500 if statements.

    If you have that sorted can you post your solution and mark the topic as resolved?
    That will help other people in the future having the same problem.

    Thanks,
    Ivan

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘301 htaccess results in 404’ is closed to new replies.