Writing Rules for Mod_Rewrite

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

Writing Rules for Mod_Rewrite

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

URL Redirecting with Mod-Rewrite
How to Create Your Own Rules

Almost Time to Start Writing Your Own Rules for URL Redirection


When you are creating your own rewrite rules with mod-rewrite, it is very important to test everything in exactly the same setting as you will be using it, on separate location before you install any htaccess on your main site (if your site has any value from being up).

Personally, I keep an extra domain, where no people or robots go, just for testing before I go live with a site, any new rules, or anything else that could cause my site to quit functioning, even if it's just for a few minutes is tested there first. (If you do not have this option, plan to create a new folder, and test from there, so your main site and content are unaffected by any rules you write.)

The first thing you need to know about your rules is where to put them. Unfortunately, the only answer anyone who does not know your site can tell you is in the htaccess file. Here is why...


By using htaccess for your mod-rewrite URL redirection, you have a lot of flexibility in what you do. You can put your htaccess file in the main site folder, and keep all of your rules there, or you can use a htaccess file for only certain folders (directories). Finally, you can use a combination of the two. Where you put them is up to you.


To avoid breaking your site for any period of time, I recommend what I wrote earlier... If you have an extra domian, put it in the main directory of that domain, then you know any URL request made will be processed by your htaccess file. If you do not have an extra domain, put your htaccess in the folder that you created to work with, this way your rules will only affect the URL's of that folder and not your entire site.


Now that you have decided where to put your file, there are some effects the location will have on your rules and conditions. Since you are using a htaccess file, this file has to be in position to 'catch' a rule (or condition) as a URL is being requested.


In the main folder, this is fairly easy, but it is necessary to keep in mind your rule (and condition if present) must be looking for the exact URL request. In a sub-folder the rules (and conditions) will only affect files that are farther into that folder. This is easier to explain with examples, so here goes.


Pretend your site is called www.yoursite.com and in www.yoursite.com, you have three folders (directories)

1. Is considered the main folder in many cases this is called 'www', 'public-something', or 'www.yoursite.com' (for now pretend it is www.yoursite.com).

2. Is a folder you created called 'pages'

3. Is a folder you created called 'more-pages'.

Further, pretend each of these folders has a two pages as shown below.

This is how your 'structure' looks:
www.yoursite.com has a total of four items:
1. index.html
2. information-1.html
3. pages
(this is a sub-folder)
4. more-pages (this is also a sub-folder)

The sub-folder 'pages' has a total of two items:
1. information-2.html
2. information-3.html


The sub-folder 'more-pages' has a total of two items:
1. information-4.html
2. information-5.html



The URL path to the folders (directories) you are pretending you have, will look like this in the address bar of a browser:

http://www.yoursite.com/index.html
http://www.yoursite.com/information-1.html

http://www.yoursite.com/pages/information-2.html
http://www.yoursite.com/pages/information-3.html

http://www.yoursite.com/more-pages/information-4.html
http://www.yoursite.com/more-pages/information-5.html


You have the option of using htaccess in any of your folders (directories), but where you put them will determine what they can affect.

If you add a htaccess file to your main folder 'www.yoursite.com' your new 'structure' will look like this:

www.yoursite.com will have a total of five items:
1. index.html
2. information-1.html
3. pages
(this is a sub-folder)
4. more-pages (this is also a sub-folder)
5. htaccess

By putting your htaccess file in www.yoursite.com you have the ability to create rules for any page or folder in your site, because all files in your site are either in this folder or 'below' it.

(EG the folder 'pages' is inside www.yoursite.com and so technically all of its contents are also in www.yoursite.com, you just can't see them, because they are in a sub-folder. (In proper terms they are 'below' the folder www.yoursite.com)


The reason you can write a rule that will affect these 'sub-folders' is that a user has to 'pass through' the folder www.yoursite.com to get to them.


If you put your htaccess file in the 'pages' file, your new 'structure' will look like this:


www.yoursite.com has a total of four items:
1. index.html
2. information-1.html
3. pages
(this is a sub-folder)
4. more-pages (this is also a sub-folder)

The sub-folder 'pages' has a total of three items:
1. information-2.html
2. information-3.html
3. htaccess


The sub-folder 'more-pages' has a total of two items:
1. information-4.html
2. information-5.html


From this location, in 'pages' you will only be able to write rules that affect information-2.html and information-3.html, which are actually in the 'pages' folder, because:


1. You do not 'pass through' the folder 'pages' on your way to open the folder 'more-pages' or any of it's contents, and they are not 'connected' to, or 'inside' of each other.


2. You are already past www.yoursite.com, before any of your rules are seen, and you can't go backward.
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 1 guest

cron