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.

Note that when forwarding your old domain to a new one, it is strongly recommended to continue hosting the domain forward with SSL and set up an .htaccess file to manually specify redirects.

To redirect an entire domain with paths but exclude a folder or file

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.

Here’s one final example, but we’re redirecting the entire website to a single page on the same domain and excluding that page from the rule to prevent a redirect loop.

This is helpful if you are shutting down a site and just need a simple message to display. Or, in my case, if a client disappears, I can’t get in touch with them, and I need to display an “account suspended” message for a while.

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.

Redirect the entire “Day and name” permalink structure to “Post name”

If you had been using the “Day and name” structure that looks like:

And you’ve switched it to just “Post name,” you can use this redirect rule:

This will redirect all of your old permalinks to the new ones quickly and easily.

Redirect http to https/SSL in WordPress

If you’d like a lightweight solution to redirect all traffic to https and use your SSL certificate on a WordPress site, look no further! This will save you the trouble of having to add a plugin. The best part: you shouldn’t even need to update it with your domain name. It should just work!

Important: add this code right before the # BEGIN WordPress  line. It may not work properly anywhere else.

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

4 Comments on “Helpful .htaccess Redirect Code That Actually Works”

  1. Thank you, Brian! I spent hours scouring the web to figure out how to redirect individual images that were renamed during a website migration project. Nothing worked until I came across your article and read through the “To manually specify redirect rules for specific paths and pages” section. After I applied the format you provided, it worked! Yes! Finally! Thank you very much for your help through this article.

    1. You’re very welcome! Yes, .htaccess redirects can be really tricky, and sometimes things that seem like they’d be super easy don’t seem to have any solutions online.

  2. Hi Brian,
    Great tutorial, thanks. However, my redirects only seem to work if the old domain is entered without “https://www” i.e. just domain.com/x. Do you have any advice you could share?
    Thanks…

    1. You are correct, that’s how it’s supposed to work. The solution is to… Not enter “https://www”.

      Or I guess I’m not understanding what the problem is? What are you trying to do that isn’t working?

Leave a Reply

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