301 redirect htaccess .htaccess

What is a 301 redirect?

A 301 redirect rule in htaccess is a permanent redirect which passes full link equity to the new URL. 301 redirects are useful when you want to change the URL of a page on your site and forward the old URL to the new URL as it has been moved permanently or move a page or a whole site to a new domain name.

When Google finds a 301 redirect, they use it as a strong signal that the redirect target URL should be the canonical URL that gets shown in search results.

How to 301 redirect a URL

1. Create an .htaccess file

.htaccess files are configuration files for Apache web servers. If you haven't already, create a file on your web hosting called ".htaccess" including the dot at the beginning of the file name.

2. Paste this line of code into your .htaccess file

Redirect 301 /old-url https://example.com/new-url

3. Add your old URL

The old URL is the version of the URL that you want to redirect visitors away from.

The old URL comes first in the redirect rule and it should be a relative URL, e.g. it should start with a / instead of the domain name. This means that redirecting from "https://example.com/old-url" should be written as "/old-url".

Change "old-url" in the example above to your page's relative URL that you want to redirect away from.

If you haven't already hidden your file extensions from your URLs with .htaccess rules like the URLs in this example, then you'll need to add the extensions in your redirect rules such as "/old-url.html".

4. Add your new URL

The new URL is the version of the URL you want people to visit and be redirected to when they visit the old URL and that you want Google to show in search results.

The new URL comes second in the redirect rule and it should contain the full URL including the domain name and protocol "http://" or "https://".

Change "https://example.com/new-url" in the example above to your new URL in full.

We have used https in the new url for the example, but make sure you have https active on your site to do this otherwise those URLs won't work. If you don't have https on your site, just use http in the second url.

For other redirects like redirecting users who visit pages that don't exist to a custom 404 page read htaccess 404.

Thanks for reading.
Ryan

Published on 1 Aug 2022