Broken permalinks and a surge in 404 errors: Differences in rewrite rules between Apache and Nginx

The main screen appears normal, but the moment you click on a post, you encounter a 404 error. This is a common occurrence after a WordPress migration. Often, the issue is not that the database is missing, but rather that the connection path for delivering the address is broken.

Especially if you have migrated from Apache to Nginx, you should start by suspecting the configuration files. Even if you copy the documents, there is no guarantee that the new server will read those files.

The post’s address is protected by both WordPress and the web server.

WordPress manages the addresses of posts, pages, categories, and tags as permalinks. It also stores structural information such as date-based or name-based formats in the database.

However, simply saving the address is not enough to open the post. When a visitor requests the address, the web server must forward that request to WordPress’s index.php file.

If WordPress tells you the room number, the web server is the information desk that guides visitors to that location.

Even if address information remains, visitors will get lost if the information desk disappears.

Why does only the main page open, but the posts return a 404 error?

If the entire website stops working immediately after a migration, it is easy to identify the cause. Conversely, if the main page loads, but posts and page addresses all return a 404 error, it is easy to mistakenly assume that there is data corruption.

In Apache, typically, the .htaccess file in the document root and mod_rewrite are used to forward requests to index.php. When saving permalink settings, these rules may be reflected in the file.

However, Nginx does not have configuration files within folders, similar to Apache’s .htaccess. You must directly specify the rules within the server or location sections.

Even if the `.htaccess` file remains in the new server, Nginx does not read that file. The existence of a file and the server’s use of that file are completely different things.

You cannot freely modify the Nginx settings in WordPress. Therefore, even if you click the “Save” button multiple times on the permalinks screen, the Nginx rules will not be automatically generated.

The “Save” button does not create any server-side rules.

Reasons why Apache rules do not work when applied to Nginx.

The issue is not simply a matter of different grammar. The two servers differ in the location where they read the rules and the way they process requests.

The `RewriteRule`, `RewriteCond`, and `[L]` directives are expressions that Apache understands. If you directly copy them into an Nginx configuration file, they will not be processed with the same meaning.

In Apache, the way an address is viewed depends on the location of the rule. In the server or VirtualHost area, paths with a leading slash are displayed, but in .htaccess, relative paths without the folder name are displayed.

Even though it may appear to be the same rule, the starting points are different. Therefore, even if a copied rule seems correct, it may not match the request.

Nginx processes rules at the server level, and if the address changes, it can find a new location. If misconfigured, the same process may be repeated, leading to a 500 error.

The correct answer is not copying, but reconstruction.

Divide the areas where 404 errors are frequently occurring into two groups.

When troubleshooting, it is necessary to separate the process of address creation from the process of request transmission. If these two processes are mixed, it is easy to only verify the saved information in the management screen and overlook the server settings.

When you save an entry in the permalinks screen, the address structure is saved within WordPress, and the rewrite rules are updated. However, this does not automatically create the server or location configurations in Nginx.

The address itself may remain intact in the database. The problem lies in the fact that Nginx may not know where to pass the request when the visitor requests that address.

In Nginx, the try_files approach is generally the key: it first looks for the actual file or directory, and if it does not exist, passes the request to index.php. Static files are handled as they are, while permalinks that do not correspond to actual files are sent to WordPress.

If this one-line flow is missing, only the main screen may remain functional. Posts, pages, and category addresses will result in a 404 error because they cannot be found.

Successful storage and server application are separate processes.

The most common areas where people get stuck when trying to fix things themselves.

When resolving issues directly, it is necessary to check both the management screen and the server settings. While the management screen may indicate that the save operation was successful, it is necessary to separately verify which configuration files were actually read.

If you are using Apache, you need to check if a .htaccess file exists in the new DocumentRoot, if AllowOverride is enabled, and if mod_rewrite is activated. If any of these are missing, the rules may not work, even if they are correctly configured.

If the `AllowOverride` directive is not enabled, the rules in the `.htaccess` file may be ignored. Even if the file content is correct, if the server does not read it, the result is the same as having a blank piece of paper.

If you are using Nginx, you need to check in which of the following: the server configuration or the location configuration, the rules are placed, and whether the actual files and directories are processed first, and whether the remaining requests are directed to index.php.

If the problem persists even after modifying the configuration file, you need to examine the path, processing order, syntax, and whether it is being executed repeatedly. Usually, there is no management screen that indicates what is wrong all at once.

Direct modification is often prone to speculation.

Fixing the rules: The reason for 500 errors.

Apache’s [L] only stops the current rule processing. Because the rule can be executed again after the internal request, if the condition is insufficient, a loop can occur where the same address is repeatedly written.

Nginx also requires attention. When using “last” in the “rewrite” directive, the address is changed, and a new “location” is searched, which can lead to repeated processing.

At this time, the result may be a 500 error, not a 404 error. The problem of not finding the address and the problem of the address processing not being completed have different symptoms.

Therefore, simply repeatedly saving permalinks is not a solution. While WordPress updates the address structure, it cannot determine which rules the server is using.

Divide the recovery order according to the type of server.

If you are migrating while continuing to use Apache, first check whether there is a .htaccess file in the new DocumentRoot. Then check that AllowOverride and mod_rewrite are enabled, and save the settings on the Permalinks screen to update the rules.

In this case, it is not necessary to create new Apache rules from scratch. However, the location of the rule file and the conditions under which the server reads the file must be correct.

If you have moved to an Nginx standalone server, you must remove the .htaccess file copy. You need to reconfigure the server or location settings to first process the actual files and directories, and then redirect any remaining requests to index.php.

After changing the settings, perform a grammar check and reapply the server. Then, open the addresses of the main posts, pages, and categories individually to confirm that it is not just one type that happens to be working.

If you are maintaining Apache, verify the Apache configuration. If you have switched to Nginx, configure the rules according to the Nginx method.

If you have also changed the domain, the task involves two steps. First, you must restore the permalinks on the new domain. Then, you must separately verify the 301 redirects, ensuring that the same path on the old domain redirects to the same path on the new domain.

If the servers are the same, check the application conditions of the existing rules. If the servers are different, rewrite the rules using the syntax of the corresponding server. This distinction prevents confusion caused by situations where a file exists but doesn’t work.

The core issue behind the 404 errors is not the WordPress data itself, but rather that the web server’s rewrite rules are missing or are being applied incorrectly to the server.

Frequently Asked Questions.

Q. Does the problem get resolved by simply pressing the “Save” button again on the permalinks screen?

A. It can help update the rules in Apache. However, Nginx configurations are not automatically generated, so you need to check the server or location settings separately.

Q. What needs to be changed when migrating from Apache to Nginx?

A. Do not copy the RewriteRule from Apache. Instead, configure Nginx to first process the actual files and directories, and then forward any remaining requests to index.php.

Q. Can this problem be easily solved using a tool? What kind of tool should be used?

A. A management tool is available to assist with checking the syntax of configuration files and verifying server logs. However, it is necessary to select the rules appropriate for either Apache or Nginx, and to manually open and verify the main address.

Q. How are 404 errors and redirects managed for a website that has undergone a migration?

A. It is necessary to verify both the rules for redirecting old URLs to new URLs, and the 404 error path that visitors may encounter.

The Redirect management add-on for LUX Content Migration manages redirects from old addresses to new addresses, as well as 404 error logs. However, the configuration of rewrite rules for Apache and Nginx, which handle permalinks, must be checked separately from this feature.

WordPress-related links

https://store.luxdigest.com
https://wpcm.luxdigest.com
https://sohoseo.luxdigest.com