Mod Rewrite Basics

Written by admin on June 15th, 2007 in Tutorials.

Apache as some of you might know is the httpd application that runs on webservers on controls, PHP and other file extansions.

mod_rewite is one function it has, and it can be used for many many things.

The part of mod_rewrite i am goning to show you is how to make fake folders on your website.
This is used when websites have

http://www.doamin.com/services/

instead of

http://www.domain.com/index.php?page=services

Now if we were to make these folders, we would have to link to images every time in different places and every page would have to be coded and if you wanted to change something you would be spending a long time doing it.

So, mod_rewrite does this by simply masking a URL. The point of this is to help search engines, such as google bots to search your site easier. Most serach engines will not look through a url such as:

http://www.domain.com/index.php?page=services

It will get content from a page but not the actual meaning of the page.

So here is how to use fake folders….

First open your .htaccess in your root folder, (normally public_html/.htaccess).

Isert the following code:

Options +FollowSymLinks
RewriteEngine On

The code above is telling apache to follow symiltaneous links. You will see further on in the tut. It is also turning the RewriteEngine On which is what we need to run this sequence.

The following code is where you create your folders.

RewriteRule ^services/ /index.php?page=services

What we have done here is told it to rewrite the folowing rule. SO when a user goes to /services/ it displays the page: index.php?page=services .
Although you can do this in PHP it is much quicker and more convienient. Also it is harder for people to acees your .htaccess file, and the variables can not be changed by the url where in PHP they can.

But the code above, if you want several folders it could be annoying typing it all again and making new rules.
So you can use the code below:

RewriteRule ^(.*)/ /index.php?page=$1

The code above is writing the following. Any folder, display Any Page.
This is done by using (.*) This simply means ANTHING. (As in PHP with *). the $1 is the variable. and will take the value of the first (.*) so if you want 2 places where you could have ANYTHING you would do:

RewriteRule ^(.*)/(.*)/ /index.php?page=$1&act=$2

Thats modRewrite for fake folders.

Hope it will help some people starting with mod_rewrite. Take a look at the advanced tutorial if you need more help.

One Response to “Mod Rewrite Basics”

  1. evenicoulddoit Says:

    Heya buddy, thats immense mate gj!

Leave a Reply

You must be logged in to post a comment.



Site Navigation SEO Services