Implementing .htaccess redirects on a WordPress website

Implementing .htaccess redirects on a WordPress website
Article posted on 4 February 2023 in Category WordPress

When using an .htaccess file to redirect URLs on a Wordpress site, it's important to understand the order in which the rules are processed. Wordpress includes its own set of rewrite rules in the .htaccess file that handle URL routing for the site. If redirect rules are placed after the Wordpress section, they may not take effect because the Wordpress rules will take precedence.

Here's an example of an .htaccess file with redirects placed after the Wordpress section:

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

# Redirects
Redirect 301 /old-page /new-page

To make the redirects work, they must be placed before the Wordpress section, like this:

# Redirects
Redirect 301 /old-page /new-page

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

In this example, the redirects are processed before the Wordpress rewrite rules, ensuring that the desired URL routing behavior is maintained.

I wont lie, this took me way longer to figure out than it should have ;-)