Mastering the htaccess file

Google Sniper

Search Engine Traffic Guide

Get Instant Access

There is a server configuration file at the root level of your Drupal 6 site called the .htaccess file. This file is a list of instructions to your web server software, usually Apache. These instructions are very helpful for cleaning up some redirects and otherwise making your site function a bit better for the search engines. In Chapter 1, The Tools You'll Need, we told Google Webmaster Tools that we wanted our site to show up in Google with or without the www in the URL. The .htaccess file allows you to do the same thing directly on your web site. Why are both necessary? In Google's tool, you're only telling Google how you want them to display your URLs; you're not actually changing the URLs on your web site. With the .htaccess file, you're actually affecting how the files are served. This will change how your site is displayed in all search engines.

Hey, why can't I can't see the .htaccess file?

In Unix/Linux Operating Systems, any file that begins with a period (.) is considered an invisible file. This means the .htaccess file wouldn't normally show up when you're viewing files on your server. Many FTP programs respect this standard and by default won't show you the files. If you want to see them, look in your FTP program's settings or preferences for Show hidden files, and make sure that it's set to show you those files. You may need to restart your FTP program and refresh the file list before you will see the .htaccess file.

out the following steps to master the .htaccess file:

Back up your .htaccess file. (It's really easy to mess it up.) Duplicate it and rename the new file to htaccess.txt or something similar.

Open your .htaccess file for editing. You'll be able to see a screen similar to the following screenshot:

<FiLesMatch (engine Iinc|info Iinstal11 module I profi LeI test IpoIshI.*sqlI theme I tpI(\.php)?Ixtmp11svn-base)$IA(code-style\ .p 11 Entries.*I Repository I Root I Tag I

t Don't show directory listings for URLs which map to a directory.

# Force simple error message for requests for non-existent favicon.ico.

t There is no end quote below, for compatibility with Apache 1.3. ErrorDocument 404 "The requested file favicon.ico was not found.

t Override PHP settings. More in sites/default/settings.php

3. Scroll to the bottom of the file and you should see a block of text that looks like this. I've highlighted the 4 lines you should be concerned with:

# If your site can be accessed both with and without the 'www.' prefix, you

# can use one of the following settings to redirect users to your preferred

# URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option:

# To redirect all users to access the site WITH the 'www.' prefix,

Carry 1.

# (http://example.com/... will be redirected to http://www.example.com/...)

# adapt and uncomment the following:

# RewriteRule *(.*)$ http://www.example.com/$1 [L,R=301]

# To redirect all users to access the site WITHOUT the 'www.' prefix,

# (http://www.example.com/... will be redirected to http://example.com/...)

# uncomment and adapt the following:

# RewriteCond %{HTTP_HOST} *www\.example\.com$ [NC]

# RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

4. The following table will help you deal with the redirection of the URLs:

To do this

Make this change

Example

Redirect non-www URLs to www

Redirect www URLs to non-www

Remove the # from the first two highlighted lines

Remove the # from the last two highlighted lines

http://example.com will redirect to http://www. example.com

http://www.example.com will redirect to

http://example.com

5. Save the .htaccess file and, if necessary, upload it to the server.

6. Test your site! If there is a problem, restore from your backup .htaccess file.

7. Don't forget to tell Google which you prefer using Google Webmaster Tools. See the Google Webmaster Tools section in Chapter 1, The Tools You'll Need, for more infomation on how to do this.

Was this article helpful?

0 0
SEO Guide for Top Rankings

SEO Guide for Top Rankings

Search engines are special sites on the web that are designed to help people find information stored on other sites. There are differences in the ways various search engines work. Learn more about this topic within this guide.

Get My Free Ebook


Post a comment