Why Your .htaccess File Redirects Never Work

For years, I kept making simple .htaccess 301 redirects for old pages on my WordPress sites, and they never seemed to work! It was incredibly frustrating, especially since I would always test out my redirects with an .htaccess checker, and they would always pass. I kept thinking maybe I just didn’t have the required mastery of regular expressions, or possibly I was just stupid.

Turns out it’s neither! Well, okay, maybe both are still true. But in this case that wasn’t why my .htaccess file wasn’t working. I could go on and build this up, but there’s really only one thing, and here it is:

Q. Why isn’t your .htaccess redirect working?

A: Because your redirect needs to go before the WordPress section of the file!

Yes, it really is that simple. Your redirects are probably formatted just fine, you just need to place the code for the redirect before the WordPress section. You still might need to include RewriteEngine On at the beginning of your file, though I usually don’t. But that’s it!

Why does this work? You see, WordPress is always trying to rewrite the URLs of pages so they look nice and pretty. If it does it to a page you wish to redirect, and it does it before your code runs, the URL will be different and of course the conditions will not match your redirect rule.

Let me show you!

Wrong

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

RewriteRule ^example_folder/$ http://brianjohnsondesign.com/website-design/ [R=301,L]

Right!

RewriteRule ^example_folder/$ http://brianjohnsondesign.com/website-design/ [R=301,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

You see, it’s really quite easy! Somehow, nobody seems to ever mention it, and I thought I would. Let me know in the comments if this was your problem, or if you have something else going on!

About Brian Johnson

Brian Johnson is a website developer and designer living in Lakeville, Minnesota with a passion for code and WordPress. He spends his days building WordPress websites for small businesses, developing new code with the online community, and living life.

One Comment on “Why Your .htaccess File Redirects Never Work”

Leave a Reply

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