Search Engine Optimization All-in-One For Dummies
Book image
Explore Book Buy On Amazon

Redirecting pages or sites on an Apache web server is very easy and a good way to ensure SEO. You do it by modifying a file on your website called the .htaccess file (note that the actual file name begins with a period). The .htaccess file is a control file that allows server configuration changes on a per-directory basis.

The file controls that directory and all the subdirectories contained within it. Usually, this file is placed in the root folder of your website. It is very important, when you edit Apache files, that your editor saves the file in UNIX format; otherwise, errors may occur.

The .htaccess file should be set up by default, but if your root folder doesn’t contain the file, have someone who understands how to build an .htaccess file create it. Be careful here. Some upload (FTP) programs hide the .htaccess. You don’t want to overwrite an existing .htaccess with your update.

Here’s an example of an .htaccess file for a site that moves from ASP to PHP and redirects the non-www version to the www version (note that where it says mydomain, you should put in your own domain):

# BEGIN
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.<i>mydomain</i>.com$
RewriteRule ^(.*) http://www.<i>mydomain</i>.com/$1 [R=301,L]
RedirectMatch 301 (.*).asp$ http://www.<i>mydomain</i>.com$1.php
</IfModule>
# END

Note that the preceding example redirects anything that is not www.mydomain.com. In other words, it also redirects subdomains, such as subdomain.mydomain.com and test.mydomain.com. This could create confusion if you’re using subdomains for test environments or other uses.

Before you start, you should make sure that you can access your .htaccess file. If you have access to your server so that you can upload and modify files, you should have no problem. (With the Apache server, modifying the .htaccess file does not require administrator-level access rights.) If you cannot access files in your web folders, call your hosting provider and request this ability (or contact the person who can access these files for you).

To edit the .htaccess file to redirect page(s) on your website, you must first know the URL(s) of each web page/site you want to redirect and the URL(s) of the new page/site where each will be redirected to. Then follow these steps:

  1. Log on to your website and, in the root web folder, locate the file called .htaccess.

    If there is no .htaccess file present, you need to create one. Again, be careful that there really is no .htaccess present and that you aren’t overwriting one.

    .htaccess is a hidden file, so you need to enable your FTP program to view hidden files to be able to see it.

  2. Open the .htaccess file by using a text editor with UNIX-style line endings such as Notepad++.

    A code editor such as Adobe Dreamweaver also handles the .htaccess file perfectly because it opens the file as text, but a simple text editor can do the job.

  3. Edit the file, as needed, being careful to follow the exact syntax required.

To add a 301 Redirect to a specific page in Apache

Add a line to the .htaccess file that tells the server what to do. The two ways to do this follow, and they both accomplish the same thing. (Note: You would substitute your own file URLs and domain name [the root part of your site’s URL] when using the examples given here.)

RedirectPermanent /<i>old-file.</i>html http://www.<i>mydomain.com/new-file.</i>html

or

Redirect 301 /<i>old-file.</i>html http://www.<i>mydomain.com/new-file.</i>html

To 301 Redirect an entire domain in Apache

To redirect an entire domain, you add a line to the .htaccess file that gives the server your instructions. A redirection from one domain to another would be written like this:

RedirectPermanent / http://www.<i>new-domain.com</i>/

To break these down, each 301 Redirect command contains three parts:

  • The first part tells the server what to do, and you can type this in two ways, either RedirectPermanent or Redirect 301.

  • The second part shows the old file’s relative path (its file location in relation to the current directory where the .htaccess file is located). If your .htaccess file is in your root web directory, you can use the file’s URL without the domain name, such as /old-file.html.

  • The third section is the full path to the new file. Starting with the http://, you want to include the complete URL (such as http://www.mydomain.com/new-file.html).

After you insert the 301 Redirect commands to redirect your pages, you need to put a blank line at the end of the file. Your server reads the .htaccess file line by line, so you have to include line advance (carriage return) character at some point to let the server know you’re finished.

About This Article

This article is from the book:

About the book authors:

Bruce Clay is considered by many to be the father of search engine optimization, or SEO. He has over 25 years’ experience coaching companies to improve their online presence and search results and increase their audience.

Kristopher B. Jones is the founder and former CEO of digital marketing agency and affiliate network Pepperjam, which he sold to eBay in 2009. He most recently founded three-time SEO agency of the year finalist LSEO.com and DIY Software company DoItYourselfSEO.com.

Bruce Clay is considered by many to be the father of search engine optimization, or SEO. He has over 25 years’ experience coaching companies to improve their online presence and search results and increase their audience.

Kristopher B. Jones is the founder and former CEO of digital marketing agency and affiliate network Pepperjam, which he sold to eBay in 2009. He most recently founded three-time SEO agency of the year finalist LSEO.com and DIY Software company DoItYourselfSEO.com.

This article can be found in the category: