homepage Welcome to WebmasterWorld Guest from 54.161.166.171
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Mod_Rewrite Beginning
Part 1 of a mod_rewrite tutorial.
jd01




msg:1517268
 11:00 am on Aug 11, 2005 (gmt 0)

Beginning Mod Rewrite

1. What mod_rewrite Does Not Do:
A. Create anything.
B. Write a 'fake' URL in a browser.
C. Change anything, except the location the request is delivered from, or the location of the information delivered to the page requested.

2. What mod_rewrite Does:
A. Delivers a different result when a specific request is made.

Example:
Someone requests the page yoursite.com/stuff.html

What You Can Do:
You can use mod_rewrite to redirect the browser to any page of your choosing, say: yoursite.com/stuff.php

-or-

You can serve the information from any page of your choosing including: yoursite.com/stuff.php without changing the location in the browser.

What you Cannot Do:
You cannot use mod_rewrite to change the address displayed in the browser... including to a more 'friendly' URL - The location displayed must either be requested or redirected to. The location does not need to contain any information, but must exist in a request.

3. Most common mistake:
People try to go backward. Example: They try to put a link to the page where the information is, then try to use mod_rewrite to change the location that appears in the browser.

4. what has to be, before you can use mod_rewrite:
A. Mod_Rewrite the module has to be installed and available on the server.
B. you must have AllowOverride set to FileInfo, or higher (All, etc.).
C. you must be able to follow sym links, usually Options +FollowSymLinks either in the httpd.conf (server configuration) or in the .htaccess file itself.
D. you must precede your rules/conditions with RewriteEngine on

5. When using conditions:
A. know a condition(s) will only effect the immediately-following rule.
B. know a condition(s) will only be read after a rule matches the pattern of a request. If the rule does not match, no condition will ever be checked.

6. When creating variables in rules to be re-used later, they are always designated by ().
A. Variables in a rule can be used (“back-referenced”) either in the right side of the rule (new location), or in any conditions.
B. They are gathered in the order they appear, and are retrieved by preceding the number of the variable with a $.

Example:
RewriteRule ^(var1)/no-var/(var2)$ /to-use-variables-type-$1-and-$2

Result:
to-use-variables-type-var1-and-var2

7.When creating variables in conditions to be re-used later, they are always designated by ()
A. Variables from a condition can be used (“back-referenced”) only on the right side of the rewrite rule.
B. They are gathered in the order they appear, and are retrieved by preceding the number of the variable with a %.
C. Only the variables created in the last-matched RewriteCond are available in the RewriteRule.

Example:
RewriteCond %{CONDITION_STUFF} ^(var1)/no-var/(var2)
RewriteRule ^no-var/no-var/no-var$ /to-use-variables-type-%1-and-%2

Result:
to-use-variables-type-var1-and-var2

* The exception to this is you can also use the %{CONDITION_STUFF} server varable in the rule, but it must appear exactly as it does in the condition:

Example:
RewriteCond %{CONDITION_STUFF} ^(var1)/no-var/(var2)
RewriteRule ^no-var/no-var/no-var$ /%{CONDITION_STUFF}-to-use-variables-type-%1-and-%2

8. When Using Regular Expressions and Conditions
It is much easier to create an infinite loop... be careful and test all uses before installing in the main directory.

9. If you are Using .htaccess
The file must be in a position where the requested file you are wanting to rewrite is located, or 'above'.

From the root yoursite.com/, you can write rules for any location in your site.

From the directory yoursite.com/stuff/, you can only affect files and pages that are farther 'in' '/stuff/' or 'below' the directory '/stuff/'.

So if your .htaccess file is in the location yoursite.com/stuff/, and you also have the location yoursite.com/more-stuff/, your rules will only effect the directory '/stuff/', but '/more-stuff/' will function as usual.

Example:
yoursite.com/stuff/anything-at-all can be affected,
yoursite.com/more-stuff/anything-at-all will not ever be effected

ASIDE:
In the .hatccess file the right side of the rule should have a preceding /.

Example:
RewriteRule ^leftside\.html$ /rightside.html [L]

In the httpd.conf file the left side of the rule must have a preceding /.

Example:
RewriteRule ^/leftside\.html$ rightside.html [L]

10. The location of your .htaccess file will affect the left side of the rules involved.

In the home or root directory, you would need the full path (from where you are) to rewrite the page /stuff/yourpage.html. The rule may look like this:

RewriteRule ^stuff/yourpage\.html$ /more-stuff/yourotherpage.html

In the folder or directory 'stuff' you would need the path to the folder or page from where you are, so to rewrite the same location as before from this location your rule might look like this:

RewriteRule ^yourpage\.html$ /more-stuff/yourotherpage.html

* Please notice the right side of the rule, or new location is the same in both cases, because any rewrite starts over from the beginning like it is a new request, so you must have the full path to the 'new' file in the 'new' location'

** Please also notice the left side of the rule does not start with /, while the right side does.

Hopefully this will add some basic clarity to the use of mod_rewrite for those who are new to the subject.

[edited by: jdMorgan at 1:40 am (utc) on Aug. 13, 2005]
[edit reason] Minor edits. [/edit]

 

jatar_k




msg:1517269
 4:53 pm on Aug 12, 2005 (gmt 0)

awesome post Justin, thanks

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved