Redirecting a Web page or site with .htaccess

This help page explains how to redirect a single web page, a directory of web pages, or an entire website to a new web address, using the .htaccess method.

See Cruzio’s redirect overview to decide which redirect method you should use.

Please proceed with caution. If you make mistakes, your website may become unreachable.

What is a .htaccess redirect, and why should I use it?

With the .htaccess (pronounced “dot-aitch-tee-access”) redirect method, also known as a “301 redirect,” you create a special configuration file on your site containing a redirect “directive” (instruction to the web server).

When someone visits the redirected web address, the web server will obey your directive and automatically redirect them to the new address.

Benefits of a .htaccess redirect:

  • Automatically redirects links and bookmarks to a web page at its old address to the new address
  • Preserves search engine rankings (such as Google PageRank) for the redirected web page
  • Lets you create multiple redirects in the same place

» back to top

Redirecting a single web page

  1. Log in to your web space, using either an FTP program (such as Filezilla or CyberDuck) or your website’s File Manager.  (Note: if you have Cruzio Classic hosting, the File Manager is not available, and you must use FTP instead.)  If you are using an FTP program, make sure it is set to view hidden files. (Otherwise, any file starting with a dot, such as the file we are about to create, will be hidden from view.)In the File Manager or your FTP program, go to the root directory (the folder containing your website files) of your website.
    Type of web hosting Name of root directory
    Website Hosting httpdocs
    Classic pub_html

     

  2. In the root directory, look for a file named .htaccess (including the dot at the beginning).If you do not already have a .htaccess file in the root directory, create a file there named .htaccess including the dot at the beginning, and with no file extension at the end.Note: if you create or copy a file named .htaccess on your computer’s hard drive, you may not be able to see the file. Files beginning with a dot are typically hidden from view. Instead, if you need to work with a file on your hard drive, name it htaccess (without the dot, and without any extension). You can rename it to .htaccess after you upload it to your web space.
  3. Open the .htaccess file for editing.To create a redirect, add the following code anywhere in the file:
    RewriteEngine On
    Redirect 301 /pathto/oldpage.extension http://yourdomain/pathto/newpage.extension
    

     

    In the code, make these changes:

    For this: Substitute this:
    pathto the path to the directory containing the page
    (for example, “produce/fruit/apples”)
    oldpage the filename of the page you are redirecting
    extension the page’s extension, if it has one
    (for example, “html” or “php”)
    yourdomain the page’s domain at the new address, even if you’re not changing the domain
    (for example, “joesfreshproduce.com”)
    newpage the filename of the new page

     

    Notice that you start the old address with only a slash “/”, with no domain. Start the new address with “http://”.

    Save the changes to your .htaccess file.

    If the file is not already in the root directory of your website, upload it there. Then, if the filename is not .htaccess, including the dot, change the filename. If there is an extension (like .txt) at the end, remove it.

  4. To view the redirect, enter the old address into the address bar of your web browser. If it does not forward right away, click the refresh button of your web browser and it should update.

Your permanent web page redirect is active!

The next time a search engine visits your site, it will update its records with the page’s new address, while preserving the page’s search rankings.

What if I only need a temporary redirect?

To create only a temporary redirect, use “Redirect 302” instead of “Redirect 301” in your .htaccess code.

» back to top

Redirecting a section of your website

We will use the .htaccess method to redirect a section (usually called a directory) of your site.

Follow the instructions for redirecting a single page, but use code like this in your .htaccess file:

RewriteEngine On
Redirect 301 /pathto/olddirectory/ http://yourdomain/pathto/newdirectory/

Note: if you redirect to a directory that does not contain an index file, you may get a 403 Forbidden error. In this case, either create an index file in the destination directory, or add this line to your .htaccess file to turn on auto-indexing:

Options +Indexes

» back to top

Redirecting your entire website to another website

We will use the .htaccess method to redirect your entire website to another site.

Follow the instructions for redirecting a single page, but use code like this in your .htaccess file:

RewriteEngine On
Redirect 301 / http://newdomain/

Make sure you include a space before and after that first lone slash.

» back to top

Redirect example: Joe’s Fresh Produce

Joe sells produce on his site, joesfreshproduce.com. He has a web page he uses to sell his tomatoes. He used to have the page in the fruit area of his site, but his site visitors missed it, because they were looking for tomatoes in the vegetables instead.

Joe decided to move the tomato page. He moved the file tomatoes.html from the fruit directory to the vegetables directory.

Joe wanted people looking for the page at its old address to find it at the new one, so he decided to create a redirect. He added this redirect code to the .htaccess file on his site, joesfreshproduce.com:

RewriteEngine On
Redirect 301 /fruit/tomatoes.html http://joesfreshproduce.com/vegetables/tomatoes.html

 

More shoppers are finding Joe’s tomatoes now. The long-time customers who had bookmarked the old tomato page are automatically redirected to the new page. Links from other sites to the old tomato page automatically go to the new page. Joe’s tomato sales are up!

» back to top