RewriteRule Rules! - The ^(Common|Uncommon)\ Mistakes$

RewriteRule Rules!

The ^(Common|Uncommon)\ Mistakes$

I see messages and posts all the time from well meaning people that have the ideal or perfect RegEx for the RewriteRule (same applies to the RegEx in redirectmatch rules) I bet you see a lot of these on the Internet! The trick is to try to keep them out of your code!

Most of these will slow down your server by making it work harder, one will cause the rule to outright fail when you think you are good!

The ^(Common|Uncommon|Silly|Stupid)\ Mistakes$

Most of these will slow down your server by making it work harder, at least two will cause the rules to outright fail, when you think you are (good|great|perfect|ideal)!

I see messages and firum posts, from well meaning people, that they "have the ideal or perfect" RegEx for the RewriteRule (same applies to the RegEx in redirectmatch rules). The "proof" they claim, is because it works!

It works! In fact, some of these mistakes make your server work and work and work - so yes, it works! However, it makes your server work more and that slows it down!

I'd bet you see a lot of these on the Internet! I see them often! The real trick is to try to keep them out of your code, and keep your server running faster!

Most of these will slow down your server by making it work harder, one will cause the rule to outright fail when you think you are good!

The Wild Cards!

^.*XYZ
.*XYZ

Let's apply a little uncommon sense! Anything you don't need means the server needs to do work to evaluate something that you don't need!

Unless specified the RewriteRule (and redirectmatch rules also) begin with an implied .* at the beginning and end. It basically says, "seek where I should start!" This is why ruled with ancors ^ and $ are faster - it knows where to go and doesn't have to seek.

The first rule above ( ^.*XYZ ) tells the server:

1. Look up ^ (Start at the Beginning)
2. Seek where I should start (from the beginning)
3. Go one character at a time until I can find the next character (X)
4. Match X or fail
5. Match Y or fail
6. Match Z or fail
7. Forget the rest! We pass!

The second rule above ( .*XYZ ) tells the server:

1. Seek where I should start (from the beginning)
2. Go one character at a time until I can find the next character (X)
3. Match X or fail
4. Match Y or fail
5. Match Z or fail
6. Forget the rest! We pass!

The right (faster) rule is ( XYZ ) tells the server:

1. Match the first letter X or fail
2. Match Y or fail
3. Match Z or fail
4. Forget the rest! We pass!

Examples:
Use: example
Don't Use: .*example
Don't Use: ^.*example


^.*
.*

I know it is a little over simplified but the above shows why you should never use the ^.* or .* at the beginning of your match string instead of just forgetting it, and let the default do it's job!

Examples:
Use: example
Don't Use: .*example
Don't Use: ^.*example


XYZ.*$
XYZ.*
.*$
.*

The same reasons apply to the end as to the beginning! Using .* or .*$ at the end of your match string will simply make your server work harder and go slower because you do not need them! Your match will work the same without them.

Examples:
Use: example
Don't Use: example.*
Don't Use: example.*$


^.*$

It should be obvious by the above that a match of .* will be faster and accomplish the same as ^.*$ and it will not make your server work so hard so it can serve your other customers more!

Examples:
Use: .*
Don't Use: ^.*$

The Anchors!

^
$

Whenever possible use anchors at one end or another or both! These really speed up the match time by giving the server a valid starting point!

The BIG EXCEPTION is don't use a wild card like .* or .+ right next to an anchor.

Examples:
Good Use: ^example
Good Use: example$
GREAT Use: ^example$
Don't Use: ^.*example
Don't Use: example.*$
Do NOT Use: ^.*example.*$

The Flags!

[R,L]
[F,L]
[G,L]
[R=301,L]

I've changed my mind on this one. The Apache documents say the R and G happen immediately, but in practice...

RewriteRule ^filipina/index\.php http://filipina.fil.net/ [R=301]

RewriteCond %{REQUEST_URI} !^/www/
RewriteRule .* - [F]

Doesn't Work!

A GET to http:example.net/filipina/index.php will get you a 403 Forbidden instead of http://filipina.fil.net/

To ACTUALLY make the above redirect work I needed to add the L flag to the 301 Redirect like this...

RewriteRule ^filipina/index\.php http://filipina.fil.net/ [R=301,L]

The [F], [R], [G], and [L] Flags should NEVER be used together in the same rule. The reason is because these four flags have one thing in common. They all start by telling the server "stop what you are doing when you finish this...and don't do any more!

I see people advising the use of [F,L] in many examples on the Internet. This basically says:

Stop what you are doing and Immediately Return a 403 Forbidden Status and don't do any more. Stop What you are doing and when you finish this rule and don't do any more.

If the server doesn't do any more after doing the F Flag, how will it be able to do the L Flag?

It is like my friend whose wife got angry at him for not answering her question. The question he didn't answer was, "Honey, are you asleep?" He was asleep!

The [F,L], [R,L], and [G,L] Flags only waste the Servers time trying to figure out what you told it to stop doing everything and also to do something more (namely stop doing everything again!).

Examples:
Good Use: [R,L]
Good Use: [R=301,L]

Good Use: [F]
Good Use: [R]
Good Use: [G]
Good Use: [L]
Don't Use: [R,L]
Don't Use: [F,L]
Don't Use: [G,L]

The Document Root!

^/example
/example
^/
/

When I started changing redirect and redirectmatch rules to the RewriteRule one BIG problem was understanding where I was! I am in Cebu, but what I mean is where is the Document Root!

http://example.com/example_dir/example.php

We all know that in the above example the red "/" is the Document Root! However in in redirect and redirectmatch rules we start things with the Document Root and in the RewriteRule we start things right AFTER the Document Root! All these do the same "match"...

redirect /span>example_dir/ ... redirectmatch ^/span>example_dir/ ... RewriteRule ^span>example_dir/ ... [R]

You will notice that the RewriteRule does NOT have the beginning "/". This is correct!

Examples:
Good Use: ^example
Don't Use: ^/example

.htaccess Priorities!

The Chicken or the Egg!

One HUGE Mistake people make that are new to the RewriteRule is about the .htaccess file.

It is kind of like the question, "Which came first, the Chicken or the Egg?" Of course, in the cycle of life the Chicken comes from the Egg and the Egg comes from the Chicken also! For those people wise enough to believe in God and the Bible it tells us that God made the birds (including the chicken!) and made it possible for them to produce seed (Eggs) after their kind!

Then God said, "...and let birds fly above the earth in the open expanse of the heavens." {21} And God created ... every winged bird after its kind; and God saw that it was good. {22} And God blessed them, saying, "Be fruitful and multiply, ...and let birds multiply on the earth." (Gen 1:20-22 NASB)

Like he Bible is a type of manual or documentation for life and explains that the chicken came first, there are also documents that deal with the RewriteRule and .htaccess priority.

Basically, a RewriteRule with a [F], [R], [G], and [L] flag happens in real time, line by line. Where as the redirect and redirectmatch rules set up something to happen after all other rules happen (AFTER the RewriteRule).

This is hard for some people to visualize. We are used to the idea than thing happen in order from top to bottom, and they do, but some things happen in real time as the server reads it and some things happen like a batch job at the close of the file!

To make this easy to understand, try putting ALL of your RewriteRule's ABOVE and BEFORE ANY redirect and/or redirectmatch rules. That is basically the way they will be handled anyway!

The Better solution is to just change all your redirect and/or redirectmatch rules into a RewriteRule that will be executed in order!

Examples:
First: RewriteRule
Then: redirect and/or redirectmatch rules
Better Use ALL: RewriteRule

RewriteRule Examples!

redirect to RewriteRule
redirectmatch to RewriteRule

The next two pages will deal with the conversion of the redirect rule to a RewriteRule and the conversion of the redirectmatch rule to a RewriteRule.

Site Information

W3C Validation:

XHTML CSS

Site hosted by:

pair
networks

Page Colors:

-324- 

Database - ID Number:

-3-- 

Sponsorship Info:

A Collaborative Effort!

~ MANY THANKS! ~

Produced By:

Domain Names by Gamot Network

A Proud Sponsor of the...

Filipina! Campaign

Cooperation Links:

ACTS-29 Network

Version Number:

~ ~ Ver 1.00 ~ ~