How to Set Up a 301 Redirect in .htaccess is one of the most important steps in maintaining your website’s SEO performance. A 301 redirect is a permanent redirection from one URL to another. It ensures that users and search engines are directed to the correct page, even after a page or domain has changed. This helps preserve search rankings, prevent broken links, and improve user experience. The .htaccess file, used on Apache servers, is a configuration file that allows you to control redirects, access rules, and website behavior easily.
In this article, you’ll learn how to set up a 301 redirect in .htaccess step-by-step. We’ll cover different types of redirects — from single pages to entire domains — and explain how to enforce HTTPS redirects for better security. You’ll also discover how to test and troubleshoot redirect issues effectively.
What Is a 301 Redirect?

A 301 redirect tells browsers and search engines that a page has permanently moved to a new location. This ensures that anyone visiting the old link is automatically taken to the new one. It’s crucial for preserving your website’s SEO value and providing a smooth user experience.
Unlike a 302 redirect, which is temporary, a 301 redirect permanently passes most of the link equity (or SEO authority) to the new URL. Use 301 redirects for long-term changes like site migrations or URL restructuring, and use 302 redirects for short-term moves or testing.
What Is the .htaccess File and Why Does It Matter?
The .htaccess file is a powerful configuration file used by Apache-based web servers. It lets you modify server behavior without changing core configuration files. You can use it to manage redirects, access controls, and security rules.
You’ll usually find the .htaccess file in the root directory of your website (such as /public_html/). Before editing it, always create a backup copy. This ensures you can restore your site quickly if something goes wrong. Also, make sure that the mod_rewrite module is enabled on your Apache server — it’s required for most rewrite-based redirects to function properly.
Also Read | How to Redirect HTTP to HTTPS in Windows IIS
How to Set Up a 301 Redirect in .htaccess
Setting up a 301 redirect in .htaccess is simple once you understand the syntax. You can use different methods depending on whether you’re redirecting a single page, an entire domain, or enforcing HTTPS.
Redirect a Single Page
To redirect a specific page to a new URL, use the following syntax:
Redirect 301 /old-page.html https://www.example.com/new-page.htmlThis command tells the server to permanently redirect /old-page.html to the specified new URL. Use this method when you’ve updated a page’s location or changed its URL structure.
Redirect an Entire Domain
If you’ve migrated your site to a new domain, you can redirect all traffic using this rule:
Redirect 301 / https://www.newdomain.com/This sends every request from your old domain to your new one. It’s ideal for domain name changes or complete website rebranding.
Redirect Using mod_rewrite Rules
For more advanced redirects, use Apache’s mod_rewrite module. It offers flexibility and pattern matching for complex redirect setups.
Example:
RewriteEngine On
RewriteRule ^old-page$ https://www.example.com/new-page [R=301,L]Here’s how it works:
- RewriteEngine On enables the rewrite engine.
- RewriteRule defines the redirect pattern.
- R=301 specifies that it’s a permanent redirect.
- L means it’s the last rule — no further rules will be processed after this one.
This method is powerful because it allows dynamic redirects based on patterns or conditions.
Redirect HTTP to HTTPS
To enforce HTTPS across your entire website, use this code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]This setup forces all HTTP traffic to use HTTPS. It enhances security, protects user data, and boosts SEO rankings since search engines prefer secure connections. Using .htaccess for HTTPS redirection ensures that every visitor automatically lands on the secure version of your site.
Also, Read | How to Fix ERR_TOO_MANY_REDIRECTS Error
Testing and Troubleshooting Redirects
After you set up a 301 redirect in .htaccess, always test it. Open your browser and visit the old URL — it should automatically redirect to the new one. You can also use online redirect checkers or tools like curl -I in the command line to confirm that the status code is 301.
If redirects aren’t working, check for syntax errors or missing modules. Redirect loops often occur when a rule unintentionally points back to itself. Clear your browser cache before retesting to ensure you’re seeing the latest changes. You can also validate your .htaccess file using Apache’s configuration test commands.
SEO Benefits of 301 Redirects
301 redirects are a cornerstone of good SEO practice. They preserve link equity from old URLs, ensuring that your search rankings remain intact after structural changes. Without proper redirects, you risk losing valuable backlinks and organic traffic.
They also help search engines understand when content has moved permanently, allowing indexing updates without penalties. To maintain site speed and crawl efficiency, avoid long redirect chains or unnecessary intermediate hops. Clean, direct 301 redirects are best for both SEO and user experience.
Conclusion
Understanding how to set up a 301 redirect in .htaccess is vital for anyone managing a website. These redirects ensure users and search engines reach the right destination even after pages or domains change. They preserve your SEO rankings, prevent broken links, and enhance user trust.
Always handle your .htaccess file with care, test each redirect thoroughly, and keep a backup before making edits. By mastering this process, you can confidently manage website migrations, URL restructuring, and HTTPS enforcement without losing performance or traffic. A well-configured .htaccess file reflects a professional and optimized web presence.