How to Create .htaccess Redirects (Most Common Use Cases)
6 Steps to Get AI to Recommend Your Brand
Whenever you move content, change domain names, or simply tweak a page’s URL, you’ll likely use a redirect. This ensures your users (and Google) get to the right page, and it preserves the SEO value from your backlinks.
You can use .htaccess redirects specifically to do this.
So the goal of this guide is to make .htaccess redirects as accessible as possible.
Here’s the plan:
- We’ll start with a walkthrough to find your .htaccess file and get ready to make changes (feel free to skip this part if you have it ready to go)
- We’ll then go through the most common .htaccess redirect use cases in detail
- Then we’ll go through a cheat sheet of the symbols and flags you can use to build custom .htaccess rules
- Finally, we’ll show you how to make sure everything is working properly and how to pinpoint issues
How to Set Up .htaccess Redirects
Before you can create redirects, you need to access your .htaccess file. This file lives on your server and controls various aspects of how your website behaves.
Editing .htaccess Through Your Hosting Control Panel
Most major hosting providers offer a file manager in their control panel that lets you edit files directly.
The general workflow looks something like this:
- Log in to your hosting account
- Find the File Manager (usually in cPanel or a similar dashboard)
- Navigate to your website’s root directory (often called public_html)
- Find the .htaccess file
Via FTP
You can also use an FTP client like FileZilla, Cyberduck, or WinSCP. To get started, download and install an FTP client. We’ll go with Cyberduck for the example below. Then connect to your server using your FTP credentials.
What About WordPress?
If you’re running a WordPress site, you usually don’t need to edit your .htaccess file to set up redirects. Instead, you can use dedicated redirect plugins like Redirection or 301 Redirects.
Before You Begin: Important Prerequisites
Before you start making changes to your .htaccess file, bear the following in mind:
- You need to have an .htaccess file: If you don’t already have an .htaccess file, you can create one in your public_html folder.
- It’s easy to make mistakes: Making changes to your .htaccess file without knowing what you’re doing can have major consequences for your site.
- The rules below are just guides: I’ve included lots of examples of redirect rules below with explanations of what they do.
- Enable mod_rewrite: Most modern hosting environments will enable the mod_rewrite module by default.
How to Redirect a Single URL
Redirecting a single URL to another is probably the most common redirect you’ll create. It’s perfect for when you’ve:
- Changed a page’s URL structure
- Merged two pages into one
- Moved specific content to a new location
Here’s the code you’ll need to add to your .htaccess file:
Redirect 301 /old-page https://yoursite.com/new-page
Permanent vs. Temporary Redirects
The 301 in our example above indicates a permanent redirect. If you’re only redirecting temporarily (like for maintenance or a seasonal promotion), use 302 instead:
Redirect 302 /sale https://yoursite.com/spring-sale
How to Redirect a Subfolder
Sometimes you need to redirect an entire section of your website rather than just a single page.
Here’s the code to add to your .htaccess file:
RewriteEngine On
RewriteRule ^old-folder/(.*)$ /new-folder/$1 [R=301,L]
Important: Order Matters
The order of your .htaccess rules matters. If you want to redirect some parts of a subfolder to one place and others elsewhere, you need to be careful.
Redirecting a Subfolder to a Different Domain
Here’s the rule to use:
RewriteEngine On
RewriteRule ^old-folder/(.*)$ https://newdomain.com/folder/$1 [R=301,L]
How to Redirect to a New Domain
A domain-wide redirect ensures anyone visiting your old domain automatically lands on your new one. Here’s the code to add to your .htaccess file on your old domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?olddomain.com$ [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301]
How to Redirect HTTP to HTTPS
Here’s the code to add to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
How to Redirect WWW to Non-WWW
Here’s the code to add to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]
Other Types of .htaccess Redirect
While the above covers the most common .htaccess redirect scenarios, the file is incredibly versatile and can handle many other specialized redirect situations.
Redirect Based on Query Parameters
RewriteEngine On
RewriteCond %{QUERY_STRING} ^product=widget$
RewriteRule ^products.php$ /widgets/? [L,R=301]
Working with Redirected URLs
You can set up custom pages for various HTTP error codes:
ErrorDocument 404 /custom-404
ErrorDocument 500 /server-error
How to Check Your Redirects Are Working
The simplest way to test your redirects is to manually check them in your browser.
Online Redirect Checkers
Consider using a tool like Semrush’s Site Audit.