Helpful .htaccess Redirect Code That Actually Works

Banner for this blog post with title and nice image of a stream

I’ve been doing this around 12 years now, and I still get so frustrated that the code I find online for .htaccess redirects never seems to work. I don’t know if everyone is just using old methods, or if maybe they only apply to certain hosting environments, or what the problem is.

So for my own reference, I have saved here some of the most useful redirect code examples. These are ones I use all the time and that actually work.

If they don’t work for you, make sure to check the bottom section of this blog post for some tips and tricks that should help you out.

Let’s get started!

*Note that all of these are 301 (permanent) redirects. If it’s temporary, change it to “302”.

To redirect an entire domain with paths to a new domain

So you would go from oldsite.com to newsite.com, and you want to include paths, e.g. oldsite.com/contact should go to newsite.com/contact.

Easy! Here it is.

You could also add a path to the end of the new site if you want everything to go there. For example, maybe you want to go from oldsite.com to newsite.com/blog/. This is how you would do that:

This one still includes pathing, so subpages will redirect to their corresponding URL at the new domain.

To redirect an entire domain with paths but exclude a folder

This is the same as the first example, but we’re going to exclude a specific folder from redirection. There are a couple situations in which you might want to do this:

  1. Certain landing pages or sections of the site will remain where they are, or possibly will be redirected elsewhere with another rule
  2. You’re hosting the domain somewhere that uses an AutoSSL method and redirects of a specific folder prevent it from issuing the SSL

Also easy! We just add a simple rule. In this example, we’re preventing the “/blog/” folder from redirecting.

Or here’s another example where we’re excluding the “/.well-known/” folder which is used by many used to issue a LetsEncrypt SSL.

This would allow the redirects to work while still issuing the SSL properly and automatically.

To redirect an entire domain but discard paths

Sometimes you just want to redirect every single page on a site to a single URL. Maybe there’s just a single landing page at the new site, or for whatever reason, you just don’t want paths included.

I’m not sure why, but this rule is impossible to find online. It’s not complicated though. Here it is!

To manually specify redirect rules for specific paths and pages

This is quite common. Maybe a new site was built elsewhere, and you just need to redirect all of the pages to their corresponding URLs at a new domain. Often, this means manually adding these rules for each page.

In the following example, the first rules do just that. Note that they require an exact match. So for example, though the first match is just “/” (indicating the home page), this rule will not redirect subpages.

For good measure, I included a rule at the bottom that will redirect all other traffic that doesn’t match a specific rule.

So to break it down, the first 301 redirect would redirect visitors from the home page (oldsite.com/) to newsite.com/landing-page. The second 301 redirect would redirect traffic from oldsite.com/contact-us/ to newsite.com/contact-us.

You would just replace these with whatever applies to you.

Redirect an entire folder, with paths

In this case, we want to redirect just a folder of our site. So maybe we just want to redirect the entire blog folder of our site, oldsite.com/blog/, over to its own domain: newsite.com. Here’s how we can do that:

Redirect an entire folder and discard paths

This would be exactly the same as the above example, but now we don’t care about the paths. This means that no matter which subpage of /blog/ a visitor lands on, they will be redirected to the root domain of newsite.com.

.htaccess Redirect Tips & Tricks

  1. Use the .htaccess tester.
    This is an awesome tool. It allows you to test out your redirects and see exactly what’s happening with them. This is useful to troubleshoot issues before you’ve actually made your .htaccess live.
  2. In WordPress, your .htaccess redirects need to go before the WordPress rules.
  3. If you need to redirect one file extension to another site-wide, I have another post about that here.
  4. Supposedly, if you have an .htaccess file in a subdirectory, files and folders within that directory will only use rules from that and completely ignore the rules in the site root. So theoretically you could exclude a folder from any rules by simply placing an empty .htaccess file in it.

And there you have it! In working with my own clients who are predominantly small businesses and organizations with WordPress sites, these examples would handle about 95% of the redirects I encounter.

Hopefully, these can save you from going down a Google / Stackoverflow rabbit hole, filled with outdated or just wrong answers.

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.

Leave a Reply

Your email address will not be published.