Redirects are often considered one of the unavoidable aspects of managing a website. A new website may not need redirects, but over time, when you change the domain, remove outdated content, or install an SSL certificate, setting up proper redirects becomes necessary to avoid losing SEO rankings and traffic.
One of the best ways to set up redirects is through .htaccess files. When you create a .htaccess 301 redirect, all users will get redirected to the new page permanently. Also, when search engines see a 301 redirect, they tend to pass all ranking signals to the new page—so you won’t lose your SEO rankings or traffic, plus your new page will get indexed quickly.
This article discusses different kinds of .htaccess redirects, how to set them up, and frequently asked questions about 301 redirects.
What is a .htaccess redirect?
An Apache redirect rule written in the .htaccess file is known as a .htaccess redirect. A .htaccess file is a configuration file that lets you enable or disable certain features of your server software without directly editing the main server configuration file, httpd.conf.
If your website is on a shared server, you might not have access to the httpd.conf file. However, you will have access to the .htaccess and can configure your redirects in the .htaccess file. They both work almost the same.
Note that redirects are to be implemented through the .htaccess file when you don’t have access to the httpd.conf file. If you are on a dedicated server, you might have access to the httpd.conf file, and you can set the redirect rules there.
.htaccess redirect codes
301 redirect
A 301 redirect is a permanent redirect. It tells bots and search engines that the page has been moved permanently. Search engines update their index when they see a 301 status code and pass the ranking signals to the new page, so the backlinks pointing to the old page will still have value. This is why 301 permanent redirects are unavoidable in many cases.
A 301 redirect is the best option when you want to:
- Migrate your website to a new domain permanently.
- Change the URL structure of your site.
- Switch from HTTP to HTTPS or force HTTPS on your site.
- Delete outdated content after publishing new content on the same topic.
- Merge closely related or relevant pages on your site.
302 redirect
A .htaccess 302 redirect is temporary—it takes visitors to the new URL for the time being and signals search engines that the move is temporary. In the case of a 302 redirect, search engines like Google index only the old page, and the ranking signals are not transferred to the new page.
A 302 redirect is better when you:
- Redirect users to a localized version of the website, such as a location-specific or language-specific version.
- Run an A/B split test.
- Want to redirect users to a new page as part of a promotion or a survey.
Where is .htaccess file located?
The .htaccess file is usually found in your site’s root directory. Depending on your hosting, the root directory may be labeled public_html, htdocs, httpdocs, or www. If you are using an addon domain, the root directory can be your domain name itself. You can use your hosting account’s file manager to find your .htaccess file location.
You might need to create a .htaccess file if you don’t find it anywhere on your server. You can create an empty text file using a text editor like Notepad and write all your redirect rules in the file. Once done, you can save the file as .htaccess and upload it to your root directory.
Setting up redirect rules in .htaccess
We have added .htaccess redirect rules for different scenarios below. To set up .htaccess redirect rules on your website, copy the relevant code below and paste it into your .htaccess file, preferably at the bottom. Replace the URLs and domain names and save the .htaccess file. Changes to .htaccess are immediate, so you should see results in no time. Just try accessing the old URL to test .htaccess.
If your 302 or 301 redirects are not working after adding redirect rules, you can try clearing your cache and reloading the page. If it still doesn’t work, you might have a syntax error. Make sure you have used proper syntax and URLs.
In the following examples, we have used mostly .htaccess 301 redirects. Note that you must replace the 301 redirect code with 302 when you want a temporary redirect.
How to redirect a single page to another page or domain
To redirect a page on your site to another page on the same website or a different website, you can paste the following lines of code in your .htaccess file.
Redirect 301 /oldpage https://domain.com/newpage
Replace ‘/oldpage’ with the location of the page you want to redirect and ‘https://domain.com/newpage’ with the URL of the page you want to redirect to.
How to redirect an entire site or domain to a new domain
Adding the following line of code in the .htaccess file redirects the entire site to a new domain.
Redirect 301 / http://www.newdomain.com/
Adding this code to the .htaccess file simply changes the domain name to ‘www.newdomain.com,’ leaving everything else as such. For example, a user accessing ‘oldsite.com/page/1/’ will be redirected to ‘www.newdomain.com/page/1/’.
How to redirect an entire site to a subfolder
You can use the following syntax if you want to redirect an entire site to a subfolder. You can use the same syntax to redirect the site to a subfolder on the same site or a subfolder on a different site—just replace the URL accordingly.
Redirect 301 / http://www.domain.com/subfolder/
When you use this code, the old domain is redirected to the new domain’s subfolder, keeping everything else the same. Someone accessing ‘olddomain.com/example/’ will be redirected to ‘domain.com/subfolder/example’.
How to redirect a subfolder to a domain
You can use the following syntax to redirect only a specific path or subfolder of your website to another domain or the original website homepage.
Redirect 301 /subfolder http://www.domain.com/
How to redirect a file or page extension without changing the name and path
When you want to change the file extension but keep everything else, such as the path and filename, the same, you can use the following syntax. It redirects an HTML file extension to a PHP file extension, keeping the same page name and file name.
RedirectMatch 301 (.*)\.html$ http://www.domain.com$1.php
How to redirect a non-www domain to a www subdomain
Adding the following syntax to your .htaccess file will redirect your non-www domain to a www subdomain.
RewriteEngine on
RewriteBase /
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
How to redirect a www subdomain to a non-www domain
To .htaccess redirect www to a non-www version of your website, you can use the following syntax. It redirects ‘www.domain.com’ to ‘domain.com’.
RewriteCond %{HTTP_HOST} ^(.\*)$ http://domain.com/$1 [L,R=301]
How to redirect HTTP to HTTPS after installing an SSL certificate
After installing an SSL certificate, redirecting all traffic to the HTTPS site is necessary to avoid duplicate content issues with search engines. You can use the following code to force HTTPS in .htaccess on your site.
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
How to redirect a subdomain to a subfolder
Use the following syntax to redirect a subdomain to a subfolder. It redirects the subdomain ‘blog.oldsite.com’ to the subfolder ‘newdomain.com/blog’ while retaining the path and query. It eliminates the extra blog path if the old path is blog.oldsite.com/blog.
RewriteEngine On
RewriteCond %{REQUEST_URI}/ blog
RewriteRule ^(.*) http://www.newdomain.com/%{REQUEST_URI} [R=302,NC]
RewriteRule ^(.*) http://www.newdomain.com/blog/%{REQUEST_URI} [R=302,NC]
.htaccess redirects: FAQs
How to use a redirect in a .htaccess file?
You can add the following code to your .htaccess file to redirect all traffic to a new domain.
Redirect 301 / http://www.newdomain.com/
Replace ‘http://www.newdomain.com/’ with the URL of the domain you want to redirect to.
What is a 301 redirect and how do I do it?
A 301 redirect is a permanent redirect, which signals browsers and search engines that the page has been moved permanently. To implement a 301 redirect, you can add a 301 redirect rule in your .htaccess file. You can refer to the ‘.htaccess redirect rules’ section above and copy the relevant syntax.
What is the path to the htaccess file?
The .htaccess file is usually placed in the root folder of a website. Depending on your hosting, the root folder may be labeled public_html, www, htdocs, httpdocs, or your domain name.
Are htaccess files executed from the top or bottom?
Like any other Apache configuration file, .htaccess files are also executed from top to bottom.
How do I redirect a URL to another URL?
To redirect a URL to another URL permanently, add the following code to your .htaccess file.
Redirect 301 /path1 http://www.domain.com/url
Replace ‘/path1’ with the path of the original URL and ‘http://www.domain.com/url’ with the URL you want to redirect to. If you want a temporary redirect, replace the 301 code with 302.
How do I redirect a page from one domain to another?
To redirect a page from one domain to another, add the following lines in your .htaccess file.
Redirect 301 /path1 http://www.domain.com/path
Replace ‘/path1’ with the original path and ‘http://www.domain.com/path’ with the URL of the page you want to redirect to. If the redirect is temporary, replace 301 with 302.
How to redirect all pages to another domain using htaccess?
To permanently redirect all pages to another domain using .htaccess, add the following code to your .htaccess file.
Redirect 301 / http://www.domain.com/
Replace ‘http://www.domain.com’ with the URL of the new domain. Note that this code replaces the domain name but retains the rest of the URL.
How to create 301 redirects from old domain to new domain?
To 301 redirect all pages from your old domain to your new domain, you can paste the following lines in your .htaccess file.
Redirect 301 / https://www.newdomain.com/
Replace ‘https://www.newdomain.com/’ with the URL of the new website. When you use this code, all users and search engines will be redirected to the new domain. Also, it replaces only the domain name and retains the rest of the URL. It will work perfectly when migrating a site without changing URL structures.
How do I redirect a whole domain?
You can set a redirect rule in your .htaccess file to redirect a whole domain to another domain. Here’s what the syntax for this redirect looks like:
Redirect 301 / https://www.newdomain.com/
Simply replace ‘https://www.newdomain.com/’ with the URL of the site you want to redirect to.