Why Your .htaccess Redirects Never Work in WordPress

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 even set up proper redirect hosting with SSL. I kept thinking maybe I just didn’t have the required mastery of regular expressions, or possibly I was just stupid.

Note that if you’re having issues with your .htaccess file on Godaddy hosting or another shared hosting provider, you may just have a different issue.

Bonus: helpful .htaccess redirect code that actually works.

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

Right!

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 Minneapolis, 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.

30 Comments on “Why Your .htaccess Redirects Never Work in WordPress”

    1. I’m glad it helped! Working with .htaccess redirects can be really difficult unless you really know what you’re doing, and sometimes it’s just little things like this that make the difference.

    1. My example might be a rewrite, though it’s irrelevant to the point in my article. The principle applies to both!

    1. Yeah it’s one of those weird things that never seems to come up and is just so hard to troubleshoot on your own. I’m glad you were able to get it working with this tip!

      1. Without getting into the drama, this competitor is an old employee of my client’s. He set up a url similar to my client’s domain name / website. He’s getting on the phone with other clients (and emailing them) and asking them to go to his url, then it redirects them to my client’s website. He then sells his services while acting like he’s part of my client’s company. He’s sort of posing as a sales rep which is why he can explain a different phone number, different email and different url. This has caused my client to lose some of his important clients and has his attorney involved, etc. It’s a long sorted story, but we need to stop him from redirecting to the site we built.

        1. Well I suppose you should be able to determine the referrer, and with a bit of coding you could actually display a message to anyone being referred to that domain. In this case, you may want to put a warning stating that if they were sent from that site, they are not working with an authorized salesperson.

          Otherwise I would think legal action would be effective, too!

  1. Thanks for confirming what I just found through trial and error.

    In my case most things worked with my redirects after the WP section, but not all. My redirects were a combination of what cpanel created and what I’d put in manually.

    redirect to https from non http worked
    redirect from different domains to http://www.example.com worked
    example.com to to http://www.example.com worked

    but what didn’t work was redirect from http://www.differentdomain.com/specificpage to http://www.example.com/specificpage – it would get to the page but keep the http://www.differentdomain.com

    When I moved the redirect above the WP section it worked.

  2. Hi,

    In the mod_rewrite documentation it is mandatory to declare ‘RewriteEngine On’ once but I also noticed that there’s no need of putting ‘RewriteEngine On’ even once, because all the rewrite rules are perfectly working anyway.

    I made some research and everybody said that is mandatory to initialize the rewrite engine with ‘RewriteEngine On’ but I still see everything is working without it.

    Do you know why?

    Cheers,

  3. I just migrated a WordPress site, and meticulously crafted my redirects before doing so only to find none of them worked.

    Of course, once I discovered (and redirected) them all using the awesome Redirection plugin, it occurred to me that maybe the redirect code needed to go before the WordPress code in the htaccess file… thanks for confirming my suspicion!

      1. Hiya Brian

        Thanks for this information!!

        I am still having issues.
        I want all pages from my old domain to redirect to my new domain. For example:

        olddomainname.com/about/ (should redirect to) newdomainname.net/about/

        Here is what I have in the htaccess file:
        RewriteEngine On
        RewriteCond %{HTTP_HOST} (w*)olddomainname\.com$ [NC]
        RewriteRule ^ http://newdomainname.net%{REQUEST_URI} [L,R=301]

        …and it works on the testing link you provided, but after clearing every browser cache I have here, it still doesn’t work.
        Any ideas?

        Thanks again!

  4. You are the fracking man! Thank you for taking the time to write this article with such a clear searchable title. It seems like every solution on wordpress.stackoverflow.com needs to be prefaced with a link to this article. CHEERS!

  5. I was pulling my hair out for 2 days trying to resolve my issue. Messing with my server config files, htaccess, multiple server reboots. This simple fix resolved my redirect issues. Thank you so much!

  6. Not working for me though
    Used code

    RewriteRule ^/tag/$ http://www.mywebsite.in/ [R=301,L]

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

Leave a Reply

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