Categories

Search Engine friendly URLs using apache’s mod_rewrite

In my last tutorial i described how to use PHP for SE friendly URLs. This time its inverse. Here you will get to know how to use Apache’s mod_rewrite for SE friendly URLs. Here we go:

Installation and Enabling mod_rewrite (Skip this if you are not the server administrator)

First on a *NIX machine, you would have to compile the module with appropriate options. This module is usually shipped with apache; but in case you want to upgrade, etc, etc.

After compiling it, to add it to the list of modules so that apache (httpd) will load it during startup use these:

apache/httpd 1.3.x :

LoadModule rewrite_module modules/mod_rewrite.so

AddModule mod_rewrite.c

apache/httpd 2.2.x :

LoadModule rewrite_module modules/mod_rewrite.so

——————-

Configuring Mod_Rewrite in httpd.conf OR .htaccess :

Now, for example there is a URL with you like ” something.com/abcd/pqrs/tpop/index.php ”

you could simply make it something.com/file.html by using this in httpd.conf OR .htaccess :

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^file.html /abcd/pqrs/tpop/index.php

—–

You can specify unlimited no. of RewriteRules.

The syntax is:

RewriteRule <^false URL/Alias required> <Original URL>

Notice the space between the two URLs!! Don’t mix up them else it won’t work!!

Now the question arises that “What do I do if I have a dynamic system which uses HTTP GET parameters to show the content ? ”

Here’s the solution for this:

consider a url like this: something.com/index.php?do=viewarticle&id=123

and you want a URL like something.com/articles/123

Now, in .htaccess OR httpd.conf use these lines:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^articles/[0-9]+ /index.php?do=viewarticle&id=$1

——-

Here [0-9]+ means there can be any number between 0 and 9 and their repetition.

You can use also a combination of other Regex expressions to achieve your goal. What i tell you here is the tail of an elephant ; the whole elephant is still remaining !!

Well, the syntax for this type is:

Options +FollowSymLinks
RewriteEngine On
RewriteRule <^Fake URL+ Regex> <Original URL and $1 for first regex then $2, & $n for nth regex>

That’s all for now!

Explore posts in the same categories: Web Hositng, Server

Comment: