Getting Started

How to Set Up a 301 Redirect in .htaccess

Learn how to properly implement a 301 redirect in your .htaccess file. Our simple guide prevents common SEO mistakes and protects your website's link equity.

Maya OkaforBy Maya Okafor·July 21, 2026·How we test

What Is a 301 Redirect and Why Use .htaccess?

A 301 redirect is a permanent instruction that tells web browsers and search engines a webpage has moved to a new URL for good. For sites hosted on Apache servers, the `.htaccess` file is the most efficient place to manage these redirects. This server configuration file acts instantly at the request level, before your website's code (like WordPress or PHP) even starts to load, making it the fastest and most reliable method for redirecting traffic.

The primary reason to use a 301 redirect is to preserve your search engine rankings. When you permanently move a page, a 301 redirect passes the vast majority of its SEO value, or 'link equity,' to the new location. This prevents you from losing the authority your old page built up. This differs significantly from a 302 redirect, which signals a temporary move and does not pass along the same SEO benefits, as search engines expect the original page to return.

Common scenarios for implementing a 301 redirect include changing a page's URL slug, migrating your entire website from HTTP to secure HTTPS, moving to a completely new domain name, or standardizing your URL structure to either use or not use the 'www' prefix. In all these cases, a permanent redirect ensures a seamless experience for users and maintains your site's visibility in search results.

Code Examples for Common 301 Redirects

The simplest redirect is for a single page. To redirect an old page to a new one, add the following line to the top of your `.htaccess` file, making sure to use the full, absolute URL for the destination. This direct command is easy to read and perfect for a few one-off changes. Remember to replace the example URLs with your actual old path and new full URL.

```apache Redirect 301 /old-page.html https://www.yourdomain.com/new-page/ ```

For more complex situations, like redirecting an entire domain or forcing HTTPS, you'll need to use the `mod_rewrite` module. First, ensure it's enabled with `RewriteEngine On`. To redirect your entire site to a new domain while keeping the URL path, use this rule. The `(.*)` captures everything after the domain, and `$1` inserts it into the new URL, while `[R=301,L]` specifies a permanent redirect and stops processing more rules.

```apache RewriteEngine On RewriteCond %{HTTP_HOST} ^olddomain\.com$ [NC] RewriteRule ^(.*)$ https://www.newdomain.com/$1 [R=301,L] ```

One of the most critical redirects enforces HTTPS across your entire site. This rule checks if the connection is not already secure (`HTTPS` is `off`) and then permanently redirects the user to the `https` version of the exact same page. Placing this in your `.htaccess` guarantees a secure connection for all visitors and is a foundational SEO practice.

```apache RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] ```

Avoiding Common .htaccess Redirect Mistakes

The most notorious issue with redirects is the 'redirect loop,' which results in a 'Too Many Redirects' browser error. This happens when a URL redirects to a second URL, which then mistakenly redirects back to the first. Always double-check your rules to ensure they have a final, single destination and don't create a circular logic chain. For example, don't redirect non-www to www, and also have another rule redirecting www to non-www.

Your `.htaccess` file is extremely sensitive to syntax. A single typo, an extra space, or a missing character can cause a 500 Internal Server Error and take your entire website offline. Before you make any changes, always download a backup copy of your existing `.htaccess` file. After saving your edits, immediately test your website in a new browser tab to confirm it still loads correctly.

Rule order is critical because the server processes `.htaccess` directives from top to bottom. More specific rules should always be placed before more general, catch-all rules. For instance, if you have a rule to redirect a single page (`/about-us`), place it before a rule that redirects an entire directory (`/about/`). If you don't, the broader rule might catch the request first, leading to an incorrect redirection.

How to Test and Verify Your Redirects

Once you’ve saved your changes, the first and most basic test is to check it yourself. Open a private or incognito browser window to bypass your cache, then type in the old URL. You should be instantly and automatically taken to the new URL. If you land on the correct destination page, you've passed the first step.

For a more technical confirmation, use a server header checker tool. Websites like `httpstatus.io` let you enter a URL and will show you the full redirect chain and HTTP status codes. For a permanent move, you want to see a single '301 Moved Permanently' status for the old URL, followed by a '200 OK' status for the final destination. If you see a 302, 307, or a chain of multiple redirects, you need to revise your rules.

If you're redirecting many URLs as part of a site migration, manual testing isn't practical. Use a website crawling tool like Screaming Frog SEO Spider (which has a free version). You can upload a list of your old URLs and have the tool crawl them. It will report back the status code for each one, allowing you to quickly spot any that aren't redirecting with a 301 or that are leading to a 404 error page.

Frequently asked

Questions readers ask about this topic

What's the difference between a 301 and a 302 redirect?

A 301 redirect is permanent, telling search engines to transfer all SEO value to the new URL. A 302 redirect is temporary, signaling that the original page will return, and therefore SEO value is not passed.

Will a 301 redirect hurt my SEO?

No, when implemented correctly, a 301 redirect helps your SEO by consolidating link equity and providing a good user experience. Problems only arise from incorrect implementation, like creating redirect loops.

Where is the .htaccess file located?

The .htaccess file is located in your website's root directory, which is usually named `public_html` or `www`. Since it's a hidden file, you may need to enable 'Show Hidden Files' in your hosting file manager or FTP client to see it.

Can I use a WordPress plugin instead of editing .htaccess?

Yes, plugins like Redirection provide a user-friendly interface for managing redirects. This is a safer option for beginners as it avoids direct file editing, though server-level .htaccess rules are technically faster.
Keep exploring · Getting Started

Where to go next on Hostilo

Newsletter

One email a month. Hosting deals, new reviews, no fluff.

Related reading