WordPress multisite – a rewrite infinite loop bugWordPress multisite – a rewrite infinite loop bug

When I create my new home page I use WordPress multisite feature. By the way, I noticed an interesting bug. When I try to view such a non-existent image from wp-content directory, the server returns an error with code 500 instead of 404. At first I thought it was a problem with hosting, but it turned out to be a bug in the default configuration of .htaccess for WordPress running as a multisite with path-based network. It looks like this:

The problem is two marked lines. They make that all the addresses to files like http://rob006.net/en/wp-content/file.jpg are treated as http://rob006.net/wp-content/file.jpg. The expression ([_0-9a-zA-Z-]+/)? is responsible for handling prefixes for sub-directories, but the problem is a ? at the end. It makes the prefix is optional, so if the http://rob006.net/wp-content/file.jpg file does not exist, server trying to internal redirect request to http://rob006.net/wp-content/file.jpg (yes, exactly the same address). Since there is no such file, server search rewrite rules again, find the same rule as before and try performs it again. That creates a redirect loop that causes the error 500. Then in the logs you can find these entries:

The solution is remove ? sign from these rules.

TL;DR

Correct content of .htaccess file looks like that:

 

One comment to “WordPress multisite – a rewrite infinite loop bug

  1. Amazing, thank you very much Robert for this post – it helps me alot!
    I have searched for the answer more than 5 hours all over the internet and tried almost everything, and only your decision worked – like charm. 🙂
    By the way, I get this error on my multisite platform only when enabled https (ssl certificate). Without ssl, even with using “?” symbol I didn’t have problems.

Leave a Reply

Your email address will not be published. Required fields are marked *