Sites

Wikipedia
SlashDot
Gentoo Linux
Loop-AES Encryption
Geoiptool

Wednesday, July 1, 2009

Mod Rewrite Explained...

In this tutorial, I'm going to be teaching what mod rewrite is and few examples of its uses. Mod rewrite is a powerful tool and one of the simplest ways to make your website more SEO friendly.

So lets get started...

What is Mod Rewrite?

Mod rewrite is a part of apache servers that can rewrite requested URLs on the fly. As it supports an endless number of rules that in turn have unlimited attached rule conditions it is very flexible and an important URL manipulation mechanism. It can be used for internet users and for search engine friendly URLs. This increases the chance of the database driven website to be indexed. Which is a very good thing for you and your website.


Enabling / Installing Mod Rewrite

The first thing we want to do is make sure that we have mod_rewrite on our server. By default, Apache has mod_rewrite installed, but not enabled. The easiest way to test if mod_rewrite is enabled on your server is to create a .htaccess file in a test directory such as /modrewrite-test/.htaccess and edit it to look like this:



Options +FollowSymLinks
RewriteEngine On

Now to test, attempt to browse to the subdirectory(test). One of two things could happen:
  1. No errors mod_rewrite engine is now enabled.
  2. 500, Internal Server Error If you get this message then mod_rewrite was not installed or enabled on your server
If you found that mod_rewrite was not installed on your server, follow these steps.
  1. Find the httpd.conf file (usually you will find it in a folder called conf, config or something along those lines)
  2. Inside the httpd.conf file uncomment the line LoadModule rewrite_module modules/mod_rewrite.so (remove the pound '#' sign from in front of the line)
  3. Also find the line ClearModuleList is uncommented then find and make sure that the line AddModule mod_rewrite.c is not commented out.
After these steps, restart your apache server:

/etc/init.d/httpd restart
now mod_rewrite should be enabled. Be sure to test mod_rewrite again once apache server has been restarted.

Using Mod Rewrite To Always Remove / And Add WWW

One easily fixed problem that many websites have is duplicate results in search engines. In any given search engine, http://www.yoursite.com/ and http://yoursite.com are two completely different websites. This is a big problem because the content in your website is identified as duplicate content, and that brings your search engine rankings down. Using mod_rewrite, we can fix this issue.

At the top of any .htaccess file, you need to turn Mod Rewrite on. This should always be at the top of your .htaccess file.(You only need to turn Mod Rewrite on once)
Also you need to set your Rewrite Base. This is normally the directory in which the .htaccess file is located. In this example the .htaccess is located in the root directory:


RewriteEngine On
RewriteBase /

To always add the www to the front of your site, add the following to your .htaccess file located in the root dir(/):

RewriteCond %{HTTP_HOST} ^yoursite.com$
RewriteRule (.*) http://www.yoursite.com$1 [R=301]


Bookmark and Share

No comments:

Post a Comment