Hostwinds Tutorials

Search results for:


Table of Contents


How Do I Create A Permanent Redirect?

Set a Permanent (301) Redirect Using .htaccess

Tags: htaccess 

How Do I Create A Permanent Redirect?

In the never-ending race to have a relevant easy to navigate site with quality content that helps drive traffic, you may find yourself needing to redesign your site or rebuild it from the ground up using a different domain name. Or perhaps you're simply wishing to expand or diversify your web presence/brand by utilizing a new domain name that still links to some of the content from your other website. Whatever the scenario, at some point, you may find yourself in need of a way to redirect content from one location to another. This is where Redirects come in. In another article, we discussed how to Set A Temporary (302) Redirect Using .htaccess. However, the focus of this article is to teach you how to set a permanent redirect using that same all-powerful .htaccess file.

How Do I Create A Permanent Redirect?

Although there are a few ways to redirect a website, such as through a website's actual code, we will be showcasing how this is accomplished using a .htaccess file. If you have not already created a .htaccess file, go check out our guide: Creating and editing a .htaccess file. That guide also covers some methods available to edit your .htaccess file. With your .htaccess file in hand (figuratively speaking), open in an editor of your choosing, you'd use the following code to redirect your entire website/domain to a new website/domain.

#Redirect current domain to a new domain
RewriteEngine On
RewriteRule ^(.*)$ http://www.your-new-domain-here.com/$1 [R=301,L]

Or

Redirect 301 / http://different-domain.com

You can also redirect your website/domain name to its www variation using the following:

#Redirect current site to its www variant
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www.yourdomain.tld$ [NC]
RewriteRule ^(.*)$ http://yourdomain.tld/$1 [R=301,L]

If you're using an SSL and you'd like to force your site to always load with the SSL add this code to your .htaccess:

#Force SSL
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.tld/$1 [R,L]

You can even redirect the www address of your site to the non www address like so:

#Redirect www to non www
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

There are other variations of redirection rules/code. However, these are generally the most commonly used. Don't forget to save your .htaccess file once you're satisfied with the edits that you have made.

If you have access to cPanel, you can create 301 redirects in your .htaccess file using the Redirect option under Domains.

Written by Michael Brower  /  June 22, 2017