.htaccess File Redirect Guide, How to redirect to new address or domain
Learn how to redirect any domain name or URL using the .htaccess file. Here is complete guide about .htaccess redirects and how to set them.
Here’s what you can do with .htaccess redirects:
- 301 and 302 Redirects
- WWW to non-WWW redirects
- Domain Name Redirect
- Force HTTPS redirects
- Improve SEO and Fix Broken Links
Redirects are essential for managing website traffic, ensuring users and search engines reach the correct pages. Whether you’re updating URLs, restructuring a site, or handling outdated links, proper redirection helps maintain SEO value and user experience.
The .htaccess file, used on Apache servers, provides a simple yet powerful way to implement redirects, including 301 (permanent) and 302 (temporary) redirects. With a few lines of code, you can efficiently redirect individual pages, entire directories, or even force HTTPS.
This guide will help you understand the basics of .htaccess redirects and how to apply them effectively for seamless website navigation.
We are working on WordPress for 7 years and many time we use .htaccess file to set domain redirects and other.
What is the .htaccess File?
The .htaccess file is a compact yet powerful configuration file used on websites running Apache web servers. It enables website owners to manage various aspects of their site’s functionality, such as redirects, URL rewriting, and access control, without altering core server settings.
This flexibility makes it an essential tool for optimizing website performance, improving security, and enhancing user experience.

What Can .htaccess Do?
The .htaccess file is a powerful configuration tool for Apache servers, allowing you to control various aspects of your website. Here are some key functions it can perform:
- Force HTTPS – Ensure all traffic uses a secure connection
- Redirect URLs – Send visitors from one page to another.
- Rewrite URLs – Change how URLs appear in the browser for better readability.
- Restrict Access – Block specific IP addresses or password-protect directories.
Types of Redirects in .htaccess
Redirects in .htaccess allow webmasters to manage URL changes efficiently. These redirects help users and search engines reach the correct pages, whether due to site restructuring, URL updates, or domain migrations. Below are the most common types of redirects used in .htaccess:
1. 301 Redirect (Permanent Redirect)
A 301 redirect indicates a permanent move from one URL to another. It transfers both users and search engine rankings to the new URL, making it the preferred method for SEO.
Example: Redirect an old page to a new one
Redirect 301 /old-page.html https://example.com/new-page.html
Use Cases:
- Moving content to a new URL permanently
- Merging duplicate pages
- Redirecting www to non-www (or vice versa)
2. 302 Redirect (Temporary Redirect)
A 302 redirect is used when a page is moved temporarily, ensuring that search engines do not update their index to the new URL.
Example: Redirect a page temporarily
Redirect 302 /temporary-page.html https://example.com/new-temp-page.html
Use Cases:
- Running A/B testing on different pages
- Temporarily redirecting traffic during website maintenance
3. Redirecting an Entire Domain
If you are moving your website to a new domain, you can redirect all pages from the old domain to the new one using a wildcard redirect.
Example: Redirect an entire domain to another
Redirect 301 / https://newdomain.com/
Use Cases:
- Changing your website domain
- Merging multiple domains into one
4. Redirecting Non-WWW to WWW (or Vice Versa)
To ensure consistency, you can force users to access your site with or without www in the URL.
Redirect non-www to www:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
Redirect www to non-www:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
Use Cases:
- Standardizing URL structure
- Preventing duplicate content issues
5. Redirecting HTTP to HTTPS (Forcing SSL)
To improve security and SEO, it’s essential to redirect all traffic from HTTP to HTTPS.
Example: Redirect HTTP to HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
Use Cases:
- Enforcing secure connections
- Meeting SSL/TLS security requirements
6. Redirecting a Specific Folder
If you need to move an entire folder to a new location, you can use a folder redirect.
Example: Redirect a folder and its contents
Redirect 301 /old-folder/ https://example.com/new-folder/
Use Cases:
- Moving a section of your website
- Reorganizing content structure
7. Redirecting Based on User Agent (Geo or Device Redirects)
You can redirect users based on their browser, location, or device type.
Example: Redirect mobile users to a mobile version
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "Android|iPhone" [NC]
RewriteRule ^(.*)$ https://m.example.com/$1 [L,R=302]
Use Cases:
- Redirecting mobile users to a mobile-optimized site
- Serving location-based content
Best Practices for .htaccess Redirects
Properly setting up redirects in .htaccess is essential for preserving website performance, user experience, and SEO rankings. Ensure you use the correct redirect type (301 for permanent, 302 for temporary), avoid redirect loops, and test changes before applying them site-wide. Implementing best practices prevents broken links, maintains traffic flow, and improves overall site efficiency.
- Use 301 redirects for permanent changes to maintain SEO rankings.
- Backup your .htaccess file before making any changes to avoid errors.
- Test redirects using tools like Chrome Developer Tools or redirect checkers.
- Avoid redirect loops, which can cause loading issues.
Common Issues and Troubleshooting
When configuring redirects in .htaccess, errors can lead to broken links, redirect loops, or SEO issues. Common problems include incorrect syntax, conflicting rules, and missing RewriteEngine On directives. To fix them, double-check your redirect rules, use absolute URLs when needed, and test changes in a staging environment before applying them to your live site.
- Redirects not working? Ensure .htaccess is enabled on your server.
- Too many redirects error? Check for conflicting rules or unnecessary loops.
- SEO concerns? Use proper status codes to prevent search engine confusion
Conclusion – URL Redirects Using .htaccess
Using .htaccess for redirects is a powerful yet straightforward way to manage URL changes efficiently. Whether you’re restructuring a website, switching to HTTPS, or handling expired pages, .htaccess provides precise control over how visitors and search engines are directed.
One of the most common uses is implementing 301 redirects, which permanently redirect old URLs to new ones, preserving SEO value and preventing broken links. Similarly, 302 redirects are useful for temporary changes without affecting search rankings. You can also force HTTPS to enhance security or redirect entire domains when migrating a website.
Properly setting up redirects in .htaccess improves user experience by ensuring seamless navigation and reducing errors. However, misconfigurations can cause redirect loops or server errors, so careful implementation is essential. Regularly testing your redirects ensures that they function correctly without affecting website performance.
By leveraging .htaccess, you can efficiently manage URL redirection while maintaining SEO integrity and user satisfaction.
Related Post – The Complete Guide to Kebab Case
Frequently Asked Questions
1. What is .htaccess and why is it important?
.htaccess is a configuration file used by Apache web servers to control various website settings, including redirects, security, and URL rewriting. It helps manage traffic, improve SEO, and enhance website performance.
2. How do I create a simple redirect using .htaccess?
To create a basic redirect, add the following line to your .htaccess file:
Redirect 301 /old-page.html https://example.com/new-page.html
This ensures that visitors and search engines are redirected to the new URL permanently.
3. What is the difference between a 301 and 302 redirect?
- 301 Redirect: A permanent redirect that transfers SEO value and rankings to the new URL.
- 302 Redirect: A temporary redirect that does not transfer SEO authority.
4. Can I redirect an entire domain to a new domain using .htaccess?
Yes, you can redirect all traffic from one domain to another with:
Redirect 301 / https://newdomain.com/
This will redirect all pages to the new domain.
5. How do I redirect all HTTP traffic to HTTPS using .htaccess?
To force HTTPS, add the following code to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
This ensures all visitors are redirected to the secure version of your site.
6. How can I redirect non-www to www (or vice versa) using .htaccess?
To redirect non-www to www, add this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
For the opposite (www to non-www), modify the condition accordingly.
7. What happens if my .htaccess redirect is not working?
If your redirects aren’t working, check for:
- Syntax errors in the .htaccess file
- Conflicting rules with other directives
- Server configuration issues preventing mod_rewrite from working
8. How do I test if my .htaccess redirects are working?
You can test redirects by entering the old URL in a browser or using online redirect checkers like httpstatus.io. You can also inspect the HTTP response headers using browser developer tools.