Apache Rewrite Rules

11,102 views
10,894 views

Published on

Basic talk about apache mod_rewrite

Published in: Technology, Design
0 Comments
9 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
11,102
On SlideShare
0
From Embeds
0
Number of Embeds
279
Actions
Shares
0
Downloads
0
Comments
0
Likes
9
Embeds 0
No embeds

No notes for slide

Apache Rewrite Rules

  1. 1. Apache Re-write Rules
  2. 2. What? <ul><li>Powerful mechanism </li></ul><ul><ul><li>Hiding Actual URLs </li></ul></ul><ul><ul><li>Redirecting </li></ul></ul><ul><ul><li>Reformatting request URLs </li></ul></ul>
  3. 3. Why? <ul><li>Search engine friendly URLs </li></ul><ul><li>User Friendly URLs </li></ul><ul><ul><li>Easy to remember </li></ul></ul><ul><li>Hide underlying technology </li></ul><ul><ul><li>index.php, index.axp, index.pl </li></ul></ul><ul><li>Hide actual path of files </li></ul>
  4. 4. What we need? <ul><li>Apache compiled with mod_rewrite module </li></ul><ul><li>mod_rewrite module should be enabled </li></ul><ul><li>RewriteEngine on </li></ul>
  5. 5. What does mod_rewrite do? <ul><li>It catches URLs that meet specific </li></ul><ul><li>conditions, and rewrites them as it was </li></ul><ul><li>told to do. </li></ul>
  6. 6. But how does it do that? <ul><li>Example: </li></ul><ul><li>RewriteRule ^/shortcut$ /complicated/url/here </li></ul><ul><li><IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^/shortcut$ /complicated/url/here </li></ul><ul><li></IfModule> </li></ul>
  7. 7. The Structure of RewriteRule <ul><li>RewriteRule Pattern Substitution [OptionalFlags] </li></ul>
  8. 8. What are OptionalFlags? <ul><li>Not mandatory </li></ul><ul><li>Surrounded in square brackets </li></ul><ul><li>Comma separated </li></ul><ul><li>Commonly used flags: </li></ul><ul><ul><li>F - Forbidden. The user will receive a 403 error. </li></ul></ul><ul><ul><li>L - Last Rule. No more rules will be proccessed if this one was successful. </li></ul></ul><ul><ul><li>R[=code] – Redirect. </li></ul></ul><ul><ul><li>NC – No Case </li></ul></ul>
  9. 9. More condition using RewriteCond <ul><li>RewriteCond directive defines a rule condition </li></ul><ul><li>Preserve RewriteRule with one or more RewriteCond directives </li></ul><ul><ul><li>RewriteEngine On </li></ul></ul><ul><ul><li>RewriteCond %{HTTP_REFERER} !^$ [NC] </li></ul></ul><ul><ul><li>RewriteCond %{HTTP_REFERER} !^http://domain.com [NC] </li></ul></ul><ul><ul><li>RewriteCond %{HTTP_REFERER} !^http://www.domain.com [NC] </li></ul></ul><ul><ul><li>RewriteCond %{HTTP_REFERER} !^http://xxx.yyy.zzz.100 [NC] </li></ul></ul><ul><ul><li>RewriteRule ^.*$ http://www.domain.com/ [R,L] </li></ul></ul>
  10. 10. Control Flow
  11. 11. Server Variables <ul><li>%{ NAME_OF_VARIABLE } </li></ul><ul><li>HTTP_USER_AGENT </li></ul><ul><li>HTTP_REFERER, </li></ul><ul><li>HTTP_HOST </li></ul><ul><li>REQUEST_METHOD </li></ul><ul><li>REMOTE_HOST </li></ul><ul><li>QUERY_STRING </li></ul><ul><li>HTTPS </li></ul><ul><li>SERVER_PORT </li></ul><ul><li>etc... </li></ul>
  12. 12. Some Examples: <ul><li>RewriteRule ^products/([0-9][0-9])/$ /product.php?prodID=$1 </li></ul><ul><li>RewriteCond %{HTTP_HOST} !^www.example.com$ [NC] </li></ul><ul><li>RewriteRule .? http://www.example.com%{REQUEST_URI} [R=301,L] </li></ul><ul><li>RewriteRule ^/blog/?(.*)$ http://blog.domain.com/$1 [P,NC,L] </li></ul><ul><li>RewriteCond %{TIME_HOUR}%{TIME_MIN} >0700 </li></ul><ul><li>RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900 </li></ul><ul><li>RewriteRule ^foo.html$ foo.day.html </li></ul><ul><li>RewriteRule ^foo.html$ foo.night.html </li></ul>
  13. 13. <ul><li>Thanks </li></ul><ul><li>:-) </li></ul><ul><li>http://webonrails.com </li></ul>

×