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
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# BEGIN WordPress RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # END WordPress RewriteRule ^example_folder/$ https://pagecrafter.com/website-design/ [R=301,L] |
Right!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# Rewrite Rules go BEFORE the WordPress Section RewriteRule ^example_folder/$ https://pagecrafter.com/website-design/ [R=301,L] # BEGIN WordPress RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # END WordPress |
30 Comments on “Why Your .htaccess Redirects Never Work in WordPress”
You saved my butt, sir!
Man you are a life saver!!! I was struggling with this for a few days and now everything is working as expected!! Thanks a lot
I’ve been struggling with this for so long. Thanks for the post! Still works in 2023
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.
this is a rewrite not a redirect.
My example might be a rewrite, though it’s irrelevant to the point in my article. The principle applies to both!
I have zero idea how no one else knew this was the problem, instantly fixed my problem
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!
I have a client who is having a problem with a competitor stealing his website information. Basically, we need to stop the following:
http://www.shady-competitor-url.com is forwarding to http://www.my-clients-website.com
(he’s a former employee of my client so he’s been stealing business)
The site is a wordpress site. Can you help me stop this?
The competitor is redirecting traffic TO your client’s website? Why exactly is that a problem?
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.
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!
Dude!!! Thank you!!! Huge Help!!! You ROCK!!!
This was exactly what was going wrong with my https migration….all good now!
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.
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,
I think it’s likely in your case that it already is on. You declare anyway just to make sure as I don’t think it’s always enabled by default.
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!
Yeah, it’s weird how nobody ever really mentions that! It’s obvious once you understand how it works, but still.
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!
Make sure the redirect rule comes before any other rules in your .htaccess file.
Works for me! Thanks! 🙂
Yep. You da bomb.
A million thank-you’s, for posting such as useful piece of information.
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!
Glad I could help out! And I agree: I needed this answer for years and somehow it was never mentioned anywhere.
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!
Sometimes it’s just the little things! Glad I could help out 🙂
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]
I’m not great with regular expressions, but when I tested your rule, it didn’t work. I think it’s just formulated wrong; nothing to do with WordPress. Try it here: http://htaccess.mwl.be/
Worked Good!!!