How To, Tutorials

Setting Up an .htaccess Redirect


Tweet about this on TwitterShare on FacebookShare on LinkedInEmail this to someone
Love this article? Make sure your friends and coworkers don't miss out!

Setting Up an .htaccess redirect header image

What is a 301 Redirect?

A 301 redirect reroutes site traffic from one place to another. If example1.com has a 301 redirect setup to send people to example2.net, all the visitors who type in “example1.com” will end up at example2.net, instead.

 

What are 301 Redirects Good For?

301 redirects are particularly useful for things like site maintenance and moving traffic to a new site.

 

Using a 301 Redirect for Site Maintenance

Using 301 redirects for site maintenance allows you to reroute traffic from all over the site to a “Sorry, our site is down for maintenance” page. 301 redirects have the capability to reroute an entire site so you won’t have to worry about making a 301 redirect for every webpage you own. Instead, you can set up one simple command that will take traffic to the maintenance page, no matter what page they land on.

A maintenance page is a good idea because it makes it easier for visitors to understand what is going on and cuts down on confusion and frustration.

Using a 301 Redirect When Moving Your Site

301 redirects also prove useful when moving your site to a new domain. Say you wanted to change your site branding, or that domain you’ve always wanted became available, and you want to change your domain to the new one. Well, a 301 redirect can help with that.

Telling your customers that your site is moving is all well and good until your visitors forget and visit the wrong site. A 301 redirect will send these misguided visitors to the new, correct destination, even though they forgot the URL.

If you’re moving pages to a new site, one at a time, you can also create single page 301 redirects. The 301 redirect would only reroute a visitor if they tried to land on the old page. The rest of the old site would be accessible, except for the pages redirecting visitors to new pages.

How Do I Create a 301 Redirect?

To create a 301 redirect, you have to be able to access your .htaccess file.

Once you access it, all you have to do is input some lines of code into your .htaccess file to create a 301 redirect.

However, we should know a little bit about the  <.htaccess file before we start messing with it.

What is .htaccess?

An .htaccess file is a configuration file that turns server features on and off. Think of it as a control panel for your server. By adding code to the .htaccess, you can “switch on” some of your server’s functionality that you wouldn’t originally have access to.

A 301 redirect is one of those functions. It will only become active if you add the appropriate code to the.htaccess file.

.htaccess Naming Conventions

Make sure your file is named .htaccess, else your 301 redirect – and all other functions in the .htaccess – will not work. The period in front of .htaccess is also required. Removing the period will result in a useless .htaccess file. The .htaccess file name should also be all lowercase.

The Disappearing .htaccess

 

The Disappearing .htaccess header photo

The period at the beginning of the file name represents a “hidden” file. That means, when you save and close the file, it will become invisible. Keep this in mind when working locally or on a server that doesn’t have a built-in .htaccess editor like pair Networks.

Why are they hidden? Because some files have too much power or are too confusing for the average user. You don’t want someone stumbling on the wrong file and accidentally making a change that could break the whole system.

How Do I Find Invisible Files?

So what happens when you need to access this file? You have to make it take off its cloak of invisibility and mingle with all the other files. Here’s how you do that:

Finding Invisible Files on Windows:

On Windows 10, go to the search box in the taskbar and type in “folder.” Then, out of the search results, select Show hidden files and folders. In the Advanced Settings section,  click Show hidden files, folders, and drives and then click OK.

Now you can see your hidden files. If you want to hide them again, simply repeat the steps and click Don’t show hidden files, folders, and drives box.

Finding Invisible Files on Mac:

To find invisible files on a Mac, you must first open Finder and navigate to the Devices section of the left sidebar. Click on your Mac’s name and then click the Macintosh HD folder. Once there, hold Command-Shift-. (period). Doing this will make all your hidden files will become visible. To make them disappear again, simply hold  Command-Shift-. (period).

How to Format a 301 Redirect in .htaccess

Congratulations! Now you’re ready to add your 301 redirect.

So open your .htaccess file. If you don’t have one, create one. Note that there is no extension for this file. It is simply .htaccess.

 

To redirect a page:

Picture of: Redirect 301 / old-file.html http://www.domain.com/new-file.html

Replace old-file.html with the filename of the page you want redirected.

Follow the old page name with the complete URL to the page you want it rerouted to.

In other words, this 301 redirect will result in all traffic to old-file.html being directed to http://www.domain.com/new-file.html.

If you want to redirect multiple pages, you can put multiple of these redirects in your .htaccess file. Just make sure each has its own line.

 

To redirect an entire domain:

Picture of: Redirect 301 / http://www.new-domain.com/

Replace http://www.new-domain.com/ with the domain you want your site redirected to. Because it does not mention the site being redirected, you have to take extra care to make sure this ends up in the .htaccess of your site. If it ends up anywhere else, it won’t work or will redirect the wrong site.

Save your changes once you have finished adding your 301 redirects to the .htaccess file.

Getting Your .htaccess On Your Server

If you have a hosting service like pair Networks, all you have to do is save your .htaccess file, and you’re finished.

If you don’t have the option to edit the .htaccess on your server, you will have to upload it instead. You can use FTP/SFTP or scp to do this.

It’s important to consider security when downloading and uploading things from your server. We recommend you use SFTP, which is more secure than FTP. SFTP makes it much harder for people to snatch your information while it’s in transit from your computer to your server.

If you’re uploading the .htaccess, make sure you’re sending it to the right place. If the .htaccess is not in the same directory as the page/site it’s affecting, it will either be ineffective or redirect the wrong site.

Getting Rid of the Redirect

If you’re using the 301 redirect for something temporary, like maintenance, you can easily turn the redirect off by removing the lines of code from the .htaccess file. Just re-upload the newest version of the .htaccess file to the server (or, in the case of pair Network customers, save the file on the server). Once the .htaccess is updated, the redirect will disappear.

Whether being used for temporary maintenance or adjust traffic to a new site, a 301 redirect can be a useful tool to keep up your sleeve. While an .htaccess file may seem intimidating, the functionality it offers more than makes up for the learning it requires. By learning how to use a 301 redirect, you have taken the first step in mastering the .htaccess.

Tweet about this on TwitterShare on FacebookShare on LinkedInEmail this to someone
Love this article? Make sure your friends and coworkers don't miss out!