.htaccess Redirects: 301, HTTPS and www
How to redirect with .htaccess: 301 vs 302, HTTPS and www redirects, changing your domain, WordPress and fixing the too many redirects error.

Contents
An .htaccess redirect is the most common way to send visitors and search engines that arrive at one address on to another address automatically. It's used to move your site to HTTPS, merge the www and non-www addresses, keep changed page addresses working or change your domain. Done right, visitors don't notice and your search rankings are preserved.
This guide covers the difference between 301 and 302, the .htaccess rules you'll need most often, what to watch out for on WordPress sites and how to fix the "too many redirects" error.
What is .htaccess?
.htaccess is a configuration file that the Apache and LiteSpeed web servers read for requests to a folder. On shared web hosting, it's usually in your site's main folder (public_html). Because its name starts with a dot, you may need to turn on the show hidden files option in some FTP clients and file managers.
Nginx doesn't read .htaccess. If you use Nginx on your own VPS, you set up redirects in the Nginx configuration; the details are in our How to install Nginx guide.
Make a copy of the file before you change it. A single typo makes the whole site return a 500 error; restore the copy and the site recovers right away.
The difference between 301 and 302
| Code | Meaning | When to use it |
|---|---|---|
301 | Permanent redirect | When a page, site or domain has moved permanently |
302 | Temporary redirect | During maintenance, a short promotion or testing |
With a 301, search engines index the new address in place of the old one. Because browsers cache 301s, it's a good habit to start with 302 while you test a redirect and switch to 301 once everything works. We cover all the HTTP status codes in our HTTP error codes article.
Redirecting a single page
Redirect 301 /old-page /new-page
Redirect 301 /promo https://example.com/deals
To move every address under a folder to a new folder:
RedirectMatch 301 ^/blog/(.*)$ /articles/$1
Redirecting HTTP to HTTPS
If your SSL certificate is installed, redirect every visitor to HTTPS:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This rule keeps the address of the page the visitor opened and changes only the protocol. We cover getting an SSL certificate in our What is an SSL certificate? article. In cPanel and DirectAdmin, you can also turn on the HTTPS redirect from the panel; use only one of the two.
The www redirect
If your site opens at both www.example.com and example.com, search engines see the same content at two separate addresses. Pick one and redirect the other.
From www to non-www:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
From non-www to www:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
If you use the HTTPS and www rules together, both redirect straight to the https:// version of the final address, so the visitor is redirected at most once. The www address needs to be set up in DNS; the details are in our What is DNS? article.
Changing your domain
If you've moved your site to a new domain, redirect every page on the old domain to the same page on the new domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301]
For this rule to work, the old domain still needs to point to your hosting, so don't let the old domain expire. After changing your domain, use the Change of Address tool in Google Search Console and update your sitemap. We cover moving a site from one host to another in our How to migrate a website guide.
.htaccess on WordPress sites
WordPress writes its own rules to the .htaccess file for permalinks:
# BEGIN WordPress
...
# END WordPress
WordPress can rewrite the section between these two lines, so add your own rules above the # BEGIN WordPress line. You can also manage redirects with a redirect plugin instead of .htaccess, but server-level rules are faster because they're applied without WordPress running.
The too many redirects error
If the browser shows the ERR_TOO_MANY_REDIRECTS error, there's a redirect loop. Common causes:
- A CDN and HTTPS: If a CDN talks to the visitor over HTTPS but to your server over HTTP, the server treats every request as HTTP and redirects it to HTTPS again. Set the CDN's SSL setting so it connects to the server over HTTPS as well. The details are in our What is a CDN? article.
- The same redirect in two places: In both the panel and the
.htaccessfile, or in both the.htaccessfile and a plugin. - Conflicting rules: For example, one rule redirects to www and another to the non-www address.
- WordPress address settings: The addresses under Settings → General must match the address you redirect to.
Testing redirects
Because browsers cache redirects, use a private window while testing, or use curl to see what the server actually returns:
curl -I http://example.com/old-page
The HTTP/1.1 301 line in the output shows the redirect type, and the Location: line shows the target address. If you see more than one Location, there's a redirect chain; redirect the old address straight to the final address.
Redirects on Nodesty web hosting
Nodesty web hosting plans run on the LiteSpeed web server; LiteSpeed reads .htaccess files, so you can use the rules in this guide as they are. Every plan includes free SSL; once your certificate is installed, you can redirect your site to HTTPS. You can edit the .htaccess file from the cPanel or DirectAdmin file manager.
Frequently Asked Questions
Should I use 301 or 302?
Use 301 if the page or site has moved permanently; search engines index the new address in place of the old one. 302 is for temporary redirects, for example during maintenance or a short promotion. Starting with 302 while you test a redirect and switching to 301 once everything works keeps browsers from caching a broken 301.
I changed my .htaccess file and the site shows a 500 error. Why?
The file contains a typo or a directive the server doesn't support. Undo the change or restore your backup and the site recovers right away. Then add the rules back one at a time to find the faulty line.
Does .htaccess work on Nginx?
No. .htaccess is a file read by the Apache and LiteSpeed web servers. Nginx doesn't read it; you set up redirects in Nginx's own configuration file with the return and rewrite directives.
What causes the too many redirects error (ERR_TOO_MANY_REDIRECTS)?
Two rules redirect to each other, or the server can't tell that the visitor arrived over HTTPS. The most common cause is a CDN connecting to the server over HTTP while the server redirects every request to HTTPS. Set the CDN's SSL mode so it connects to the server over HTTPS as well, and make sure you haven't turned on the same redirect both in the panel and in the .htaccess file.
Do redirects affect SEO?
Properly done 301 redirects carry most of the old address's ranking signals over to the new one. What causes problems is redirect chains, redirecting to unrelated pages and deleting pages without any redirect at all. Redirect each old address to its closest new equivalent in a single step.
I removed the redirect but the browser still redirects. Why?
Browsers cache 301 redirects. Clear the browser cache or try in a private window. You can use the curl -I command to see what the server actually returns.
About the author
Nodesty Team · Infrastructure & Support
The Nodesty team runs our servers, network and DDoS protection day to day and answers customer support requests around the clock. These guides come from that work.
Related articles

WordPress Upload Size Limit: How to Increase It
Fix The uploaded file exceeds the upload_max_filesize directive in php.ini: raise the WordPress upload limit in cPanel, DirectAdmin, .user.ini or on a VPS.

502 Bad Gateway: Causes and How to Fix It on Nginx
Getting 502 Bad Gateway? Read the Nginx error log, restart PHP-FPM or your app, fix the upstream port or socket, raise pm.max_children, check Cloudflare.

How to Reset the WordPress Admin Password When Locked Out
Locked out of WordPress? Reset the admin password with the lost password link, WP-CLI, phpMyAdmin or MySQL, or create a new admin user from the terminal.