How to Set Up a Redirect in WordPress via .htaccess
If you’re comfortable taking a more hands-on approach, you can skip plugins entirely and create redirects by editing your site’s .htaccess file.
(Note that this only applies to WordPress sites running on Apache, not Nginx.)
This file tells your server how to handle certain requests, and it’s often used for things like permalinks and redirect rules.
It can be faster than installing a plugin, and if you already have lots of plugins on your site, it limits how much extra “bloat” you add to it.
In other words: your site might run faster.
The catch is that one wrong line of code can bring your whole site down. So I strongly recommend you consult your developer if possible. Or, at the very least, take a lot of care when doing it yourself.
How to Access .htaccess
The usual method to edit your .htaccess file involves an FTP client. This is a “file transfer protocol” program that lets you access your server’s files from your computer.
There are lots of options, like Cyberduck, FileZilla, and WinSCP.
Cyberduck – FTP
Check out this guide to .htaccess files for a walkthrough of how to do this.
In many cases, you can edit your .htaccess file right inside your hosting control panel (like cPanel or a similar dashboard).
Look for a “File Manager” or something similar:
File Manager
Then, look in your site’s root folder (often called public_html) for your .htaccess file.
It might be hidden by default—there’s usually an option in your settings to change that.
File Manager – htaccess
You’ll see lines of code that might look a bit intimidating, but don’t panic.
You don’t need to edit any of them. We’ll just be adding some additional lines of code to our .htaccess file.
Before You Start
For some redirects, you’ll need to add a few lines of code like this:
Options +FollowSymLinks
RewriteEngine On
You don’t need to worry too much about what this does, but essentially it lets you add certain rules that’ll execute specific types of redirects. (Some hosts might enable the first line by default, but it’s fine to add it here anyway.)
You don’t need these for a basic single redirect, but you will for the more complex ones.
You also only need to add it once in your .htaccess file (provided you don’t also have “RewriteEngine Off” somewhere else in the file). But I’ve included it in each of the instances below that require it.
Finally, it’s important to note that these .htaccess rules don’t “create” any new pages.
You need to ensure the pages you’re redirecting to are live when you add the rule. Otherwise the redirect will lead to an error page.
With that in mind, here are the most common types of WordPress redirects you can add to your .htaccess file. In each case, add them at the start of your .htaccess file.
Free resource: To make things even easier, download our free .htaccess WordPress redirects cheat sheet.
Redirecting a Single Page
To redirect a single page using your .htaccess file, add this line of code, replacing the /old-url and /new-url parts with your own URLs:
Redirect 301 /old-url https://yourdomain.com/new-url
Note: The first part (/old-url) should be the relative form of the URL. But the second part should be the full URL, including the protocol (https://) and domain name.
Redirecting a Single Folder
When you want to redirect a folder (e.g., moving a /blog category over to your /news category), use this code:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^blog/(.*)$ /news/$1 [L,R=301]
Again, you’d replace the blog and news parts with your own folder names.
But what do all the other parts mean?
You don’t need to know the specifics, as there are lots of possible combinations. But this particular case does the following:
^ means “start looking at the beginning of the URL path”
blog/ is the folder we’re redirecting from
(.*) captures everything after blog/ (think of it like copying it to the clipboard)
$ basically says “that’s the end of what we’re looking for”
/news/ is the new folder we’re redirecting to
$1 essentially “pastes” everything we “copied” with (.*) into the new location
L means stop processing rules for this specific request once it matches this rule
R=301 means it’s a 301 redirect (as opposed to 302, for example)
So, it would redirect a URL like:
https://yourdomain.com/blog/post-1
To:
https://yourdomain.com/news/post-1
Redirecting to a New Domain
Redirecting to a new domain is where things can get tricky. Having done this myself, I strongly recommend you speak to your developer first.
But if you plan to do it yourself, here’s one example of a rule you can add to your .htaccess file (obviously subbing in your specific old and new domains, with or without www as required):
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?olddomain.com [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301]
As with redirecting folders, there are a lot of symbols in there. I won’t go over them all individually, but here are a few important parts:
RewriteCond %{HTTP_HOST} essentially means “check what domain the user is trying to access”
^(www.)? matches either www. or nothing at the start of the domain—this ensures it catches both www.olddomain.com and olddomain.com
[NC] means “not case sensitive,” so OLDDOMAIN.com would also match
^(.*)$ captures the entire URL path after the domain (like /about or /contact)
So this code would redirect:
https://olddomain.com/about > https://newdomain.com/about
https://www.olddomain.com/blog > https://newdomain.com/blog
https://olddomain.com/product/item > https://newdomain.com/product/item
This maintains the same structure for each URL but on the new domain.
You can tweak some of the symbols to catch or ignore specific parts of the URL. But again, it’s probably best to seek help from your developer if you need a more precise solution.
Remember: This .htaccess rule doesn’t create any pages on the new site.
If you try to redirect “olddomain.com/example-page” but there’s no “newdomain.com/example-page” or its URL is slightly different (e.g., newdomain.com/example-page1), the user will get a 404 error.
That’s why you should create a redirect map before starting a site move. For more on this, see our website migration checklist.
Redirecting HTTP to HTTPS
You can also redirect your website from HTTP (unsecure) to HTTPS (secure) via .htaccess. (You’ll, of course, need a valid SSL certificate.)
However:
You might not need to. There’s a good chance your web host offers a method to do this for you. Or you might already have a plugin that does it.
Domains – Force HTTPS
In either case, you shouldn’t then add a separate .htaccess rule to redirect to HTTPS. Doing so can actually lead to issues.
But if you do want to add it, here’s the code:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
You can copy and paste this rule directly into your .htaccess file without subbing anything in.
Redirecting WWW to Non-WWW
As with redirecting to HTTPS, you can usually redirect from the www. version of your website without touching your .htaccess file.
One method is through your WordPress dashboard’s “General” settings tab. Just enter the URL you want to use for your domain name in the “WordPress Address” and “Site Address Boxes”:
WP – Redirect
But if you want to do this via .htaccess, here’s the code to add (you can copy and paste it in without changing anything):
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]
Remember: You only need to add one instance of “RewriteEngine On” in your .htaccess file.
Pros and Cons of Using .htaccess for WordPress Redirects
The main advantages of using .htaccess to redirect WordPress URLs include:
- Fewer plugins, less bloat: Every plugin you install can slow down your site. Changes to
.htaccessdon’t rely on lots of extra code running in the background. - Extra control: You can set up advanced redirect rules, and you can be very specific about what does and doesn’t redirect.
- Fast execution: Because
.htaccessrules run at the server level, your redirects happen quickly.
But there are a few drawbacks:
- Risk of breaking your site: One small typo can cause major errors. Double-check everything, and always keep a backup.
- No built-in tracking: Unlike plugin dashboards that log errors and hits, you won’t see which URLs have triggered the redirect unless you check your server logs.
- Potential performance impact: While the actual redirects might be fast, adding lots of
.htaccessrules could impact overall performance and page speed (although it won’t be noticeable in most cases).
A big caveat here, though, is that which method is right for you will depend on how many redirects you need to implement and the nature of them.
Need to redirect a couple of pages, and want an easy way to do it as needed?
The likely small performance impact of a single plugin is probably worth it for the flexibility and convenience.
Redirecting one URL?
- It’s probably most efficient to add it to your
.htaccessfile (it’s just one line of code). - Performing a massive site migration with thousands or millions of redirects?
- For massive sites, it’s probably not worth taking any risks trying to use a plugin or editing your
.htaccessfile. This is where you’ll likely want to seek expert assistance.
How to Check Your WordPress Redirects Are Working
Whether you set your redirects up using a plugin or via .htaccess, I recommend testing to make sure they work as soon as you’ve implemented them. This lets you fix errors before they impact your users (or your SEO).
You can do this manually by checking the URLs if you didn’t create that many redirects.
But if you have lots of them, you can use a tool like Semrush’s Site Audit to check for issues across your entire site.
You just set up your audit and once it runs, you’ll head to the “Issues” tab.
Search for “links” to bring up any relevant issues:
Site Audit – Issues – Links
Pay particular attention to these errors and warnings:
- Internal links are broken
- Pages returned 4XX status code
- Redirect chains and loops
- Pages with a broken canonical link
- Links couldn’t be crawled (incorrect URL formats)
- Links on HTTPS pages lead to HTTP page
These often occur when you implement lots of redirects and set up some (or all) of them incorrectly.
Partner with our Digital Marketing Agency
Ask Engage Coders to create a comprehensive and inclusive digital marketing plan that takes your business to new heights.
