Wednesday, December 2, 2009

Url rewriting rule for .htaccess

Their are some rule that should be follow when we use url rewriting. Their are solution with example and definition of all parameter and tell how to use them in url rewriting.


Rewrite rules are broken down into 4 simple blocks.

  1. Call to action
  2. Pattern
  3. Rewrite
  4. Command Flag.
Example
    RewriteRule ^dir/([0-9]+)/?$ /index.php?id=$1 [L]


Call to action: RewriteRule
Pattern: ^dir/([0-9]+) /?$
Rewrite: /index.php?id=$1
Command Flag: [L]



Call to action Block


if you ever see a 500 error on your site it mostlikely due to a bad line of code in your .htaccess file. The only way to screw this up is to spell RewriteRule incorrectly or leave out the space between this and the starting of the pattern block.




Pattern Block


This one little piece of the mod rewrite is where the power is. In the pattern block of the rewrite rule we use regular expressions to detect the requested file name or uri and from this we can extract key parts to pass to the rewrite block.


Pattern Matching metacharacter Definitions


Char.
Definition
\
Use before any of the following characters to escape or null the meaning or it. \* \. \$ \+ \[ \]
^
Start matching at this point
$
End point of the match
.
Any character
[]
Starts a class
|
Starts alternative match this|that would mean match this or that
()
starts a back reference point
?
match 0 or 1 time Quantifier
+
match atleast 1 or more times Quantifier
*
match 0 to infinite times Quantifier
{}
match minimum to maximum Quantifier {0,3} match up to 3 times
Class Definitions []


Char.
Definition
^
Negates the class. [^A-Z]+ means don't match any uppercases
\
Use before any of the following characters to escape or null the meaning or it. [\+]+
-
Range for matching [0-9]+ [a-zA-Z]+


Example:-
     RewriteRule ^category/([0-9]+)\.htm$ /category.php?cat_id=$1 [L]


The best to use is the negated class since .+ will pick up a / since a / is defined as any given character. The [-a-zA-Z0-9]+ would just take up too much computing power over the long run. Remember the more you define the more strain there is on the system. Since a search for every thing but a / ([^/]+) requires less computing power it's not only fast it most optimal.




Rewrite Block
This part is a piece of cake. Now that we've used the pattern block to reference our matches ([0-9]+) we need to rewrite to the url and add the references as needed.
Remember a reference is anything that was picked up in the () in the rewrite.
To call a reference you just add a $ follow by the reference number. This all goes in order like so. Below we'll make 3 references.
RewriteRule ^dir/(.*)/(.*)\.(.htm|.html)$ /$1/$2.$3 [R=301,L]
Rewrites using a 301 redirect
dir/some/folder/file.htm to /some/folder/file.htm
You can mix up the references if you want like so:
RewriteRule ^dir/(.*)/(.*)\.(.htm|.html)$ /$2/$1.$3 [R=301,L]
you can also not call a reference like so:
RewriteRule ^dir/(.*)/(.*)\.(.htm|.html)$ /$2/$1.php [R=301,L]



Command Flag Block (Optional)
Ok I didn't tell you this is optional because half of you would skip this part. Learning the different Command Flags is a must.
The command flag definitions are as follows:

Char.
Definition
[R]
Redirect you can add an =301 or =302 to change the type.
[F]
Forces the url to be forbidden. 403 header
[G]
Forces the url to be gone 401 header
[L]
Last rule. (You should use this on all your rules that don't link together)
[N]
Next round. Rerun the rules again from the start
[C]
Chains a rewrite rule together with the next rule.
[T]
use T=MIME-type to force the file to be a mime type
[NS]
Use if no sub request is requested
[NC]
Makes the rule case INsensitive
[QSA]
Query String Append use to add to an existing query string
[NE]
Turns of normal escapes that are default in the rewriterule
[PT]
Pass through to the handler (together with mod alias)
[S]
Skip the next rule S=3 skips the next 3 rules
[E]
E=var sets an enviromental variable that can be called by other rules

EAJBJZSSMQEB

No comments:

Post a Comment