Code Snippet: 301 redirects using .htaccess

A 301 redirect is a permanent way to let search engines and visitors know that an old page on your website has been replaced by a new one.

If you would like to create your own redirects using your website’s htaccess, open your .htaccess file (typically found at the root of your website) and add the following code:

Redirect 301 /myveryoldpage.html http://www.yoursite.com/myverynewpage.html
Redirect 301 /login/ http://www.yoursite.com/account/

If you would like to redirect your entire website to a new website and you are keeping the exact same page structure you can add the following code to your old website:

Redirect 301 / http://mynewwebsite.com

As an example, the code above will cause http://myoldwebsite.com/services/glossary.html to redirect to http://mynewwebsite.com/services/glossary.html so there is no need to worry about creating a separate redirection for each page.

One thing to note is that .htaccess redirections only work on Apache servers.