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.
1 2 |
RewriteEngine on RewriteRule (.*) https://newsite.com/$1 [R=301,L] |
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:
1 2 |
RewriteEngine on RewriteRule (.*) https://newsite.com/blog/$1 [R=301,L] |
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:
- Certain landing pages or sections of the site will remain where they are, or possibly will be redirected elsewhere with another rule
- 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.
1 2 3 |
RewriteEngine on RewriteRule ^blog - [L] RewriteRule (.*) https://newsite.com/$1 [R=301,L] |
Or here’s another example where we’re excluding the “/.well-known/” folder which is used by many used to issue a LetsEncrypt SSL.
1 2 3 |
RewriteEngine on RewriteRule ^.well-known - [L] RewriteRule (.*) https://newsite.com/$1 [R=301,L] |
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.
1 2 3 |
RewriteEngine on RewriteRule ^page-to-exclude.html - [L] RewriteRule ^(.*)$ https://example.com/page-to-exclude.html [R=301,L] |
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!
1 2 |
RewriteEngine on RewriteRule ^(.*)$ http://newdomain.com/ [R=301,L] |
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.
1 2 3 4 5 6 7 8 9 10 |
Options +FollowSymLinks RewriteEngine On # Redirect pages manually RedirectMatch 301 ^/$ https://newsite.com/landing-page RedirectMatch 301 ^/contact-us/$ https://newsite.com/contact-us RedirectMatch 301 ^/best-web-developer-ever/$ https://newsite.com/pagecrafter # Redirect all other traffic RewriteRule ^(.*)$ https://newsite.com/landing-page-2 [R=301,L] |
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:
1 2 |
RewriteEngine On RedirectMatch 301 ^/blog/(.*) https://newsite.com/$1 |
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.
1 2 |
RewriteEngine on RedirectMatch 301 /blog/(.*) https://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:
1 |
https://pagecrafter.com/2023/10/04/sample-post/ |
And you’ve switched it to just “Post name,” you can use this redirect rule:
1 2 |
RewriteEngine On RedirectMatch 301 ^/\d{4}/\d{2}/\d{2}/(.*) /$1 |
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.
1 2 3 4 5 |
# HTTPS / SSL Redirect - Brian Johnson - PageCrafter.com RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE] Header always set Content-Security-Policy "upgrade-insecure-requests;" |
.htaccess Redirect Tips & Tricks
- 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. - In WordPress, your .htaccess redirects need to go before the WordPress rules.
- If you need to redirect one file extension to another site-wide, I have another post about that here.
- 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.
4 Comments on “Helpful .htaccess Redirect Code That Actually Works”
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.
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.
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…
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?