Mod_Rewrite: How it Works and Where to Start

Information and tutorials covering what you can and can't do with mod_rewrite, regular expressions, creating rules, htaccess placement.

Mod_Rewrite: How it Works and Where to Start

Postby Mad Mod_Rewriter » Thu Jan 19, 2006 1:34 pm

URL Redirection Mod-Rewrite
How it Works and Where to Start

How URL Redirection with Mod-Rewrite Works

Mod-Rewrite works by sitting on your server in a file called htaccess, and "catching" requests for URL's. It then check these URL request against a series of rules and conditions you have set. If the request meets any of the rules set (and conditions if you have them included), it applies then necessary changes to the URL, then reprocesses the request with the changes you have directed.


The easiest way to understand it is to picture your hard disk set-up. It is a series of files and folders. Now imagine being able to put a file called htaccess inside of any of your folders to tell your computer how to respond to a request.

Further, imagine, if inside of this htaccess file, you could say things like:

"If someone asks for the file, 'stuff', give them the file 'new-stuff' instead, but don't tell them I switched the files." [OR]

"If someone besides my friends, John, Sally, or Myself, tries to open any of the files, give them a blank screen that says... FORBIDEN." [OR]

"If somebody asks for the file 'current-information', tell them we changed that to 'new-information', and take them to it, then open it for them."

This is what redirection with mod-rewrite does. It just works on the internet, and instead of names, it uses URL's (addresses with words in them) and IP addresses (EG 111.111.222.222).


The most common mistake people make when thinking of URL redirection with mod-rewrite, is they believe it creates something, or changes something... It does not. What it does is gives something, other than what was requested, when a specific request is made. The fun part is... Most of the time, with rewriting you get to decide wether to tell or not.

Defining Mod-Rewrite URL Redirects

Sometimes, like when you move a page or file on your site, you will want to let people and search engines know you changed the URL, so you change the address that is in the browser and send a code to tell every one, "Hey it's not here anymore, we moved it over there."

This is called an external redirect, meaning it can be seen externally in the browser, or history by any person or search engine that visits the site. (These are usually either a 301-code, which says 'the file has moved permanently', or a 302-code, which says the 'file has moved temporarily'.)

Other times, like when you use php or other dynamic generation, and have 'stuff' after the question mark of a URL that is hard for a search engine to read, (if they can read it at all):
http://yoursite.com/index.php?stuff=show-this
You will want to keep quiet about where the information is coming from.

To do this you create a link that goes to a page that does not exist (fake):
http://yoursite.com/stuff/show/this.html (fake)

Then you tell your server, through a htaccess file, when somebody clicks on the fake link to
http://yoursite.com/stuff/show/this.html
the information they want is at:
http://yoursite.com/index.php?stuff=show-this (real)

Your server then gives the information from
http://yoursite.com/index.php?stuff=show-this (real)
While the browser shows the location as
http://yoursite.com/stuff/show/this.html

Since you don't want to tell everyone where the 'real' information is, you tell the server to just give it to them, and not to send a code that says anything different. (It is Important to notice the words the php file (real) needs are in the fake address, URL.)

This type of redirect is known as internal or silent. Meaning only you, and your server internals know that you switched the files. (This type of redirect when used properly, returns a 200-code, which says 'found'.)

Where to Start URL Redirecting with Mod-Rewrite

The best place to start with mod-rewrite URL redirection is with what you have, your site. Deciding on what you would like to accomplish and what your goals are is essential.

As powerful as mod-rewrite is, it cannot help you if your rule is only 'close' to what you need, it must be exact. Keep in mind that something as simple as a '-' or a number that is unaccounted for can cause your rule or condition to fail, or even generate an ugly site-wide 500-Server Error.

Make sure you look at all of the following, before you start writing rules. (Anything else that affects the navigation or function of your site, should also be taken into account.)

Look at your address bar... What does it look like?

What do you want it to look like?

What pages do you link to?

What is the structure of your links?
directory relative: "page/stuff.php"
server relative: "/page/stuff.php"
absolute: "http://www.mysite.com/page/stuff.php"

Do you use query strings?
(stuff after the ? EG http://yoursite.com/file.php?variable=stuff)

If so, are the query strings (variables) you need consistant (EG all letters), or do they have other characters at times (EG letters-hyphen-numbers001)?

How are your graphics linked (relative or absolute)?

Are you trying to shorten long queries, or just be more search engine friendly?

The most important part of making URL redirection with mod-rewrite work, is to make sure you know what you need it to do. Sometimes it is more efficient to change the way you pass or receive variables than it is to come up with a rule for every possible situation.
Mad Mod_Rewriter
 
Posts: 26
Joined: Thu Jan 12, 2006 1:53 pm
Location: The Office

Return to The Basics of Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 3 guests

cron