A blend of programming and seo

Improving seo with apache mod_rewrite

What is mod_rewrite?

Mod_rewrite is a rewriting engine (based on regular-expressions) built into the apache webserver and it is used to rewrite urls dynamically. The URL manipulations can depend on various tests, of server variables, environment variables, HTTP headers, or time stamps. Even external database lookups in various formats can be used to achieve highly granular URL matching.

How to install it

Apache by default comes with the mod_rewrite module installed but it is not enabled. So if you have Apache installed on your own server, you will need to enable it.

If you need to install apache on your system, there are many free, easy install packages available:

Xamphttp://www.apachefriends.org/en/xampp.html
apache2triadhttp://apache2triad.net/
apachePHPMysqlhttp://apachephpmysql.narhoz.ru/
EasyWebServerhttp://e.w.s.free.fr/index_fr.php
FoxServhttp://sourceforge.net/projects/foxserv/

Setting it up

Once installed, mod_rewrite basically relies on one file for all it’s functionality. It’s called .htaccess. This file should be placed in the root directory of your website.

A simple Redirect

Place the following in a .htaccess file:

RewriteEngine on
RewriteRule ^test\.html$ test2.html

RewriteEngine on should always be placed at the beginning of all your .htaccess files.

Note: If you are using a hosting provider, you may have to place the following line in your file (under rewrite_engine on): RewriteBase /

Script details:

  • ^ is used before a URL. If a relative URL is used, it starts in the same directory as the .htaccess file
  • $ is used for the end of a string that will be matched.
  • \ is used to escape the period, periods need the \ before them if they are not going to be part of the actual rule (in this case, it is part of the filename).

This script will redirect all access from test.html to test2.html. IE: if a user goes to http://www.yoursite.com/test.html, they will be automatically forwarded to http://www.yoursite.com/test2.html

Improving your website SEO

An easy and simple way to generate more keyword related traffic is the following:

RewriteRule ^keyword_1.htm$ /myscript.php [NC,L]

In this example, all requests to keyword_1.htm are actualling going to myscript.php.

Other interesting uses

A) Blocking a specific Ip addressing from accessing your website.

RewriteCond %{REMOTE_ADDR} ^(W\.X\.Y\.Z)$
RewriteRule ^/* http://www.yoursite.com/sorry.htm [L]

Replace w.x.y.z with the IP you would like to block and http://www.yoursite.com/sorry.htm with the redirected URL.

B) Block/redirect a site that is linking to you

RewriteCond %{HTTP_REFERER} ^http://www\.blockedsite\.com [NC]
RewriteRule ^/* http://www.yoursite.com/sorry.htm [L]

Replace http://www.blockedsite.com/ with site you do not want linking to you, and http://www.yoursite.com/sorry.htm with the redirected URL.

C) preventing people from linking to your images

RewriteCond %{HTTP_REFERER} !^http://(www\.)?yoursite\.com [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} ^http://.*$
RewriteRule \.(png |gif | bmp | jpe?g|)$ /images/stopstealing.png [L]

Replace http://www.blockedsite.com/ your site, and /images/stopstealing.png with an image path of choice.

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • Reddit
  • Twitter
  • HackerNews
  • StumbleUpon
  • Technorati

2 comments

1 James { 08.12.09 at 11:46 pm }

Have you found a decent free URL rewriter for IIS?

2 Justin (rawseo) { 08.13.09 at 8:39 am }

I have heard that this one works pretty well:

http://urlrewriter.net/

But, I haven’t tried it myself.

Leave a Comment