mod_rewrite
Uses
Uses

•   Obscure original URLs
Uses

•   Obscure original URLs
•   Clean URLs
Uses

•   Obscure original URLs
•   Clean URLs
•   SEO (Seriously.)
Uses

•   Obscure original URLs
•   Clean URLs
•   SEO (Seriously.)
•   Redirection
Uses

•   Obscure original URLs
•   Clean URLs
•   SEO (Seriously.)
•   Redirection
•   Access control
Where can I place rewrite
        rules?
Where can I place rewrite
            rules?
•   httpd.conf (or included config files)
Where can I place rewrite
            rules?
•   httpd.conf (or included config files)
•   VirtualHost
Where can I place rewrite
            rules?
•   httpd.conf (or included config files)
•   VirtualHost
•   Directory*
Where can I place rewrite
            rules?
•   httpd.conf (or included config files)
•   VirtualHost
•   Directory*
•   .h...
Where can I place rewrite
            rules?
•   httpd.conf (or included config files)
•   VirtualHost
•   Directory*
•   .h...
Where can I place rewrite
            rules?
•   httpd.conf (or included config files)
•   VirtualHost
•   Directory*
•   .h...
Directives
Directives
•   RewriteEngine
Directives
•   RewriteEngine
•   RewriteOptions
Directives
•   RewriteEngine
•   RewriteOptions
•   RewriteRule
Directives
•   RewriteEngine
•   RewriteOptions
•   RewriteRule
•   RewriteCond
Directives
•   RewriteEngine
•   RewriteOptions
•   RewriteRule
•   RewriteCond
•   RewriteLog
Directives
•   RewriteEngine
•   RewriteOptions
•   RewriteRule
•   RewriteCond
•   RewriteLog
•   RewriteBase
Directives
•   RewriteEngine
•   RewriteOptions
•   RewriteRule
•   RewriteCond
•   RewriteLog
•   RewriteBase
•   ...and ...
Exhibit A

#   Default WordPress rewrite rules

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
Rewrite...
Exhibit A, Explained
 #   Default WordPress rewrite rules

 # Turn rewrite engine on
 RewriteEngine On

 # Base all rewrit...
RewriteEngine on | off
RewriteEngine on | off


•   Enables or disables the runtime rewriting engine
RewriteEngine on | off


•   Enables or disables the runtime rewriting engine
•   Required inside VirtualHost and .htaccess
RewriteRule pattern substitution [flags]


•   Can occur more than once
•   Processed in order
•   First rule processed on ...
RewriteRule pattern substitution [flags]
RewriteRule pattern substitution [flags]



•   PCRE (Perl-Compatible Regular Expressions)
RewriteRule pattern substitution [flags]



•   PCRE (Perl-Compatible Regular Expressions)
•   Matches the URL path
RewriteRule pattern substitution [flags]



•   PCRE (Perl-Compatible Regular Expressions)
•   Matches the URL path
•   Rew...
Regex



(?:(?:rn)?[ t])*(?:(?:(?:[^()<>@,;:".[]000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:
[t])*(?:[^()<>@,;:" .[] 000-031]+(?:(?:(?:rn)?[t])+|Z|(?=[["()<>@,;:".[]]))|[([^
[]r]|.)*](?:(?:rn)?[ t])*))*(?:,@(?:(?:rn...
[t])*(?:[^()<>@,;:" .[] 000-031]+(?:(?:(?:rn)?[t])+|Z|(?=[["()<>@,;:".[]]))|[([^
[]r]|.)*](?:(?:rn)?[ t])*))*(?:,@(?:(?:rn...
Regex          ®

It’s Not That Hard™
RewriteRule pattern substitution [flags]
RewriteRule pattern substitution [flags]


Can be one of several things:
RewriteRule pattern substitution [flags]


Can be one of several things:
 •   file-system path
RewriteRule pattern substitution [flags]


Can be one of several things:
 •   file-system path
 •   URL path
RewriteRule pattern substitution [flags]


Can be one of several things:
 •   file-system path
 •   URL path
 •   Absolute U...
RewriteRule pattern substitution [flags]


Can be one of several things:
 •   file-system path
 •   URL path
 •   Absolute U...
RewriteRule pattern substitution [flags]
RewriteRule pattern substitution [flags]

•   Affect behavior of the Rule or Condition
RewriteRule pattern substitution [flags]

•   Affect behavior of the Rule or Condition
•   Contained in square brackets
RewriteRule pattern substitution [flags]

•   Affect behavior of the Rule or Condition
•   Contained in square brackets
•  ...
RewriteRule pattern substitution [flags]

•   Affect behavior of the Rule or Condition
•   Contained in square brackets    ...
RewriteRule pattern substitution [flags]

•   Affect behavior of the Rule or Condition
•   Contained in square brackets    ...
RewriteRule pattern substitution [flags]
RewriteRule pattern substitution [flags]
          C    Chain this rule to the next rule
          E    Set environment var...
RewriteRule pattern substitution [flags]
          C    Chain this rule to the next rule
          E    Set environment var...
RewriteRule pattern substitution [flags]
          C    Chain this rule to the next rule
          E    Set environment var...
RewriteRule pattern substitution [flags]
          C    Chain this rule to the next rule
          E    Set environment var...
RewriteRule pattern substitution [flags]
          C    Chain this rule to the next rule
          E    Set environment var...
RewriteRule pattern substitution [flags]
          C    Chain this rule to the next rule
          E    Set environment var...
RewriteRule pattern substitution [flags]
          C    Chain this rule to the next rule
          E    Set environment var...
RewriteRule pattern substitution [flags]
          C    Chain this rule to the next rule
          E    Set environment var...
RewriteRule pattern substitution [flags]
          C    Chain this rule to the next rule
          E    Set environment var...
RewriteCond variable pattern [flags]


•    HTTP Server Variables (HTTP_HOST, REQUEST_URI)
•    Pattern that must match the...
RewriteCond variable pattern [flags]
RewriteCond variable pattern [flags]
            HTTP_REFERER

             HTTP_HOST

            REMOTE_ADDR

           ...
RewriteCond variable pattern [flags]
            HTTP_REFERER

             HTTP_HOST

            REMOTE_ADDR

           ...
RewriteCond variable pattern [flags]
            HTTP_REFERER

             HTTP_HOST

            REMOTE_ADDR

           ...
RewriteCond variable pattern [flags]
            HTTP_REFERER

             HTTP_HOST

            REMOTE_ADDR

           ...
RewriteCond variable pattern [flags]
            HTTP_REFERER

             HTTP_HOST

            REMOTE_ADDR

           ...
RewriteCond variable pattern [flags]
            HTTP_REFERER

             HTTP_HOST

            REMOTE_ADDR

           ...
RewriteCond variable pattern [flags]
            HTTP_REFERER

             HTTP_HOST

            REMOTE_ADDR

           ...
RewriteCond variable pattern [flags]
            HTTP_REFERER

             HTTP_HOST

            REMOTE_ADDR

           ...
Exhibit A, Explained (Again)
     #   Default WordPress rewrite rules

     # Turn rewrite engine on
     RewriteEngine On...
non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [...
non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [...
non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [...
non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [...
non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [...
non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [...
non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [...
RewriteOptions inherit
RewriteOptions inherit

•   Inherit the configuration of the parent
RewriteOptions inherit

•   Inherit the configuration of the parent
•   In per-virtual-server context, maps, conditions and...
RewriteOptions inherit

•   Inherit the configuration of the parent
•   In per-virtual-server context, maps, conditions and...
Example
Example
in httpd.conf
Example
 in httpd.conf
...


RewriteRule ^(.*)$ index.php

...


<VirtualHost 192.168.1.120:80>
   ServerName zomgbacon.co...
RewriteBase URL-path
RewriteBase URL-path


•   Sets the base URL for per-directory rewrites
RewriteBase URL-path


•   Sets the base URL for per-directory rewrites
•   URLs are NOT directly related to physical filen...
RewriteBase Example
RewriteBase Example
in .htaccess file
RewriteBase Example
in .htaccess file
#
#   /abc/def/.htaccess -- per-dir config file for directory /abc/def
#   Remember: ...
RewriteLog log-path
RewriteLog log-path

•   Logs rewrites
RewriteLog log-path

•   Logs rewrites
•   Level of logging can be tuned
RewriteLog log-path

•   Logs rewrites
•   Level of logging can be tuned
•   Relative paths are relative to DocumentRoot
RewriteLog log-path

•   Logs rewrites
•   Level of logging can be tuned
•   Relative paths are relative to DocumentRoot
•...
RewriteLogLevel level
RewriteLogLevel level

•   Integer value 0-9
RewriteLogLevel level

•   Integer value 0-9
•   0 == disabled
RewriteLogLevel level

•   Integer value 0-9
•   0 == disabled
•   9 == log nearly everything
RewriteLogLevel level

•   Integer value 0-9
•   0 == disabled
•   9 == log nearly everything
•   More verbose, greater im...
RewriteLogLevel level

•   Integer value 0-9
•   0 == disabled
•   9 == log nearly everything
•   More verbose, greater im...
Logging Examples
Logging Examples
in httpd.conf
Logging Examples
 in httpd.conf
...


<VirtualHost 192.168.1.120:80>
ServerName zomgbacon.com
DocumentRoot /home/bacon/pub...
fin
Upcoming SlideShare
Loading in …5
×

mod_rewrite

1,819 views
1,777 views

Published on

A wonderful introduction to the world of mod_rewrite

Published in: Technology
1 Comment
4 Likes
Statistics
Notes
No Downloads
Views
Total views
1,819
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
Downloads
87
Comments
1
Likes
4
Embeds 0
No embeds

No notes for slide
  • mod_rewrite

    1. 1. mod_rewrite
    2. 2. Uses
    3. 3. Uses • Obscure original URLs
    4. 4. Uses • Obscure original URLs • Clean URLs
    5. 5. Uses • Obscure original URLs • Clean URLs • SEO (Seriously.)
    6. 6. Uses • Obscure original URLs • Clean URLs • SEO (Seriously.) • Redirection
    7. 7. Uses • Obscure original URLs • Clean URLs • SEO (Seriously.) • Redirection • Access control
    8. 8. Where can I place rewrite rules?
    9. 9. Where can I place rewrite rules? • httpd.conf (or included config files)
    10. 10. Where can I place rewrite rules? • httpd.conf (or included config files) • VirtualHost
    11. 11. Where can I place rewrite rules? • httpd.conf (or included config files) • VirtualHost • Directory*
    12. 12. Where can I place rewrite rules? • httpd.conf (or included config files) • VirtualHost • Directory* • .htaccess
    13. 13. Where can I place rewrite rules? • httpd.conf (or included config files) • VirtualHost • Directory* • .htaccess *Certain restrictions apply.**
    14. 14. Where can I place rewrite rules? • httpd.conf (or included config files) • VirtualHost • Directory* • .htaccess *Certain restrictions apply.** **Use .htaccess files
    15. 15. Directives
    16. 16. Directives • RewriteEngine
    17. 17. Directives • RewriteEngine • RewriteOptions
    18. 18. Directives • RewriteEngine • RewriteOptions • RewriteRule
    19. 19. Directives • RewriteEngine • RewriteOptions • RewriteRule • RewriteCond
    20. 20. Directives • RewriteEngine • RewriteOptions • RewriteRule • RewriteCond • RewriteLog
    21. 21. Directives • RewriteEngine • RewriteOptions • RewriteRule • RewriteCond • RewriteLog • RewriteBase
    22. 22. Directives • RewriteEngine • RewriteOptions • RewriteRule • RewriteCond • RewriteLog • RewriteBase • ...and more! (that we won’t cover)
    23. 23. Exhibit A # Default WordPress rewrite rules RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L]
    24. 24. Exhibit A, Explained # Default WordPress rewrite rules # Turn rewrite engine on RewriteEngine On # Base all rewrites on ‘/’ URL RewriteBase / # If the requested file does not exist RewriteCond %{REQUEST_FILENAME} !-f # ...or if no directory exists... RewriteCond %{REQUEST_FILENAME} !-d # Rewrite the URL to /index.php RewriteRule . /index.php [L]
    25. 25. RewriteEngine on | off
    26. 26. RewriteEngine on | off • Enables or disables the runtime rewriting engine
    27. 27. RewriteEngine on | off • Enables or disables the runtime rewriting engine • Required inside VirtualHost and .htaccess
    28. 28. RewriteRule pattern substitution [flags] • Can occur more than once • Processed in order • First rule processed on URL path • Subsequent rules processed on previous output • Think of chaining commands together in bash
    29. 29. RewriteRule pattern substitution [flags]
    30. 30. RewriteRule pattern substitution [flags] • PCRE (Perl-Compatible Regular Expressions)
    31. 31. RewriteRule pattern substitution [flags] • PCRE (Perl-Compatible Regular Expressions) • Matches the URL path
    32. 32. RewriteRule pattern substitution [flags] • PCRE (Perl-Compatible Regular Expressions) • Matches the URL path • RewriteCond required to match anything else
    33. 33. Regex (?:(?:rn)?[ t])*(?:(?:(?:[^()<>@,;:".[]000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:
    34. 34. [t])*(?:[^()<>@,;:" .[] 000-031]+(?:(?:(?:rn)?[t])+|Z|(?=[["()<>@,;:".[]]))|[([^ []r]|.)*](?:(?:rn)?[ t])*))*(?:,@(?:(?:rn)?[t])*(?:[^()<>@,;:". [] 000-031]+(?: (?:(?:rn)?[t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*)(?:.(?: (?:rn)?[t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[t])+|Z|(?=[["()<>@,;:".[]]))| [([^[]r]|.)*](?:(?:rn)?[ t])*))*)*:(?:(?:rn)?[t])*)?(?:[^()<>@,;:".[] 0 00- 031]+(?:(?:(?:rn)?[t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]| .|(?:(?:rn)?[t]))*"(?: (?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[t])+|Z| (?=[["()<>@,;:".[]]))|"(? :[^"r]|.|(?:(?:rn)?[t]))*"(?:(?:rn)?[ t])*))*@(?:(?:r n)?[ t])* (?:[^()<>@,;:".[]000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))| [([^[]r]|.)*](?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[t])*(?:[ ^()<>@,;:".[] 000-031]+ (?:(?:(?:rn)?[t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*))* >(?:(?:rn)?[t])*)(?:,s*( ?:(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[t])+|Z|(?=[ ["()<>@,;: ".[]]))|"(?:[^"r]|.|(?:(?:rn)?[t]))*"(?:(?:rn)?[ t])*)(?:.(?:( ?:rn)? [t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^ "r]|.|(?:(?:rn)?[t]))*"(?:(?:rn)?[ t ])*))*@(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)? Regex [t])*)(? :.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[]000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[ ["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[t])*))*|(?: [^()<>@,;:".[] 000-031]+ (?:(?:(?:rn)?[t])+|Z|(?=[["()<>@,;:".[ ]]))|"(?:[^"r]|.|(?:(?:rn)?[t]))*"(?:(?:r n)?[ t])*)*<(?:(?:rn) ?[t])*(?:@(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[t])+|Z|(? =[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[t])*)(?:.(?:(?:rn) ?[ t])*(?: [^()<>@,;:".[]000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]| .)*](?:(?:rn)?[t])*))*(?:,@(?:(?:rn)?[ t])*(?:[^()<>@,;:".[]000-031]+(?:(?:(?:rn)? [ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[t])*)(?:.(?:(?:rn)? [ t] )*(?:[^()<>@,;:".[]000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^ []r]|.)*](?:(?:rn)?[ t])*))*)*:(?:(?:rn)?[t])*)? (?:[^()<>@,;:".[] 000-031]+(?: (?:(?:rn)?[t])+|Z|(?=[["()<>@,;:". []]))|"(?:[^"r]|.|(?:(?:rn)?[t]))*"(?:(?:rn)? [ t])*)(?:.(?:(?: rn)?[t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[ ["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[t]))*"(?:(?:rn)?[ t]) *))*@(?:(?:rn)? [ t])*(?:[^()<>@,;:".[]000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^ []r]|.)*](?:(?:rn)?[t])*)(?: .(?:(?:rn)?[ t])*(?:[^()<>@,;:".[]000-031]+(?:(?: (?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[t])*))*>(?:( ?: rn)?[ t])*))*)?;s*)
    35. 35. [t])*(?:[^()<>@,;:" .[] 000-031]+(?:(?:(?:rn)?[t])+|Z|(?=[["()<>@,;:".[]]))|[([^ []r]|.)*](?:(?:rn)?[ t])*))*(?:,@(?:(?:rn)?[t])*(?:[^()<>@,;:". [] 000-031]+(?: (?:(?:rn)?[t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*)(?:.(?: (?:rn)?[t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[t])+|Z|(?=[["()<>@,;:".[]]))| [([^[]r]|.)*](?:(?:rn)?[ t])*))*)*:(?:(?:rn)?[t])*)?(?:[^()<>@,;:".[] 0 00- 031]+(?:(?:(?:rn)?[t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]| .|(?:(?:rn)?[t]))*"(?: (?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[t])+|Z| (?=[["()<>@,;:".[]]))|"(? :[^"r]|.|(?:(?:rn)?[t]))*"(?:(?:rn)?[ t])*))*@(?:(?:r n)?[ t])* (?:[^()<>@,;:".[]000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))| [([^[]r]|.)*](?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[t])*(?:[ ^()<>@,;:".[] 000-031]+ (?:(?:(?:rn)?[t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*))* >(?:(?:rn)?[t])*)(?:,s*( ?:(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[t])+|Z|(?=[ ["()<>@,;: ".[]]))|"(?:[^"r]|.|(?:(?:rn)?[t]))*"(?:(?:rn)?[ t])*)(?:.(?:( ?:rn)? [t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^ "r]|.|(?:(?:rn)?[t]))*"(?:(?:rn)?[ t ])*))*@(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)? Regex [t])*)(? :.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[]000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[ ["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[t])*))*|(?: [^()<>@,;:".[] 000-031]+ (?:(?:(?:rn)?[t])+|Z|(?=[["()<>@,;:".[ ]]))|"(?:[^"r]|.|(?:(?:rn)?[t]))*"(?:(?:r n)?[ t])*)*<(?:(?:rn) ?[t])*(?:@(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[t])+|Z|(? =[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[t])*)(?:.(?:(?:rn) ?[ t])*(?: [^()<>@,;:".[]000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]| .)*](?:(?:rn)?[t])*))*(?:,@(?:(?:rn)?[ t])*(?:[^()<>@,;:".[]000-031]+(?:(?:(?:rn)? [ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[t])*)(?:.(?:(?:rn)? [ t] )*(?:[^()<>@,;:".[]000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^ []r]|.)*](?:(?:rn)?[ t])*))*)*:(?:(?:rn)?[t])*)? (?:[^()<>@,;:".[] 000-031]+(?: (?:(?:rn)?[t])+|Z|(?=[["()<>@,;:". []]))|"(?:[^"r]|.|(?:(?:rn)?[t]))*"(?:(?:rn)? [ t])*)(?:.(?:(?: rn)?[t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[ ["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[t]))*"(?:(?:rn)?[ t]) *))*@(?:(?:rn)? [ t])*(?:[^()<>@,;:".[]000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^ []r]|.)*](?:(?:rn)?[t])*)(?: .(?:(?:rn)?[ t])*(?:[^()<>@,;:".[]000-031]+(?:(?: (RFC 822 Compliant Email Validation) (?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[t])*))*>(?:( ?: rn)?[ t])*))*)?;s*)
    36. 36. Regex ® It’s Not That Hard™
    37. 37. RewriteRule pattern substitution [flags]
    38. 38. RewriteRule pattern substitution [flags] Can be one of several things:
    39. 39. RewriteRule pattern substitution [flags] Can be one of several things: • file-system path
    40. 40. RewriteRule pattern substitution [flags] Can be one of several things: • file-system path • URL path
    41. 41. RewriteRule pattern substitution [flags] Can be one of several things: • file-system path • URL path • Absolute URL
    42. 42. RewriteRule pattern substitution [flags] Can be one of several things: • file-system path • URL path • Absolute URL • - [dash] (no substitution)
    43. 43. RewriteRule pattern substitution [flags]
    44. 44. RewriteRule pattern substitution [flags] • Affect behavior of the Rule or Condition
    45. 45. RewriteRule pattern substitution [flags] • Affect behavior of the Rule or Condition • Contained in square brackets
    46. 46. RewriteRule pattern substitution [flags] • Affect behavior of the Rule or Condition • Contained in square brackets • Comma-separated
    47. 47. RewriteRule pattern substitution [flags] • Affect behavior of the Rule or Condition • Contained in square brackets [NC] • Comma-separated
    48. 48. RewriteRule pattern substitution [flags] • Affect behavior of the Rule or Condition • Contained in square brackets [NC] • Comma-separated [NC, QSA, L]
    49. 49. RewriteRule pattern substitution [flags]
    50. 50. RewriteRule pattern substitution [flags] C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Don’t process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
    51. 51. RewriteRule pattern substitution [flags] C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Don’t process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
    52. 52. RewriteRule pattern substitution [flags] C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Don’t process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
    53. 53. RewriteRule pattern substitution [flags] C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Don’t process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
    54. 54. RewriteRule pattern substitution [flags] C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Don’t process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
    55. 55. RewriteRule pattern substitution [flags] C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Don’t process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
    56. 56. RewriteRule pattern substitution [flags] C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Don’t process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
    57. 57. RewriteRule pattern substitution [flags] C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Don’t process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
    58. 58. RewriteRule pattern substitution [flags] C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Don’t process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
    59. 59. RewriteCond variable pattern [flags] • HTTP Server Variables (HTTP_HOST, REQUEST_URI) • Pattern that must match the given variable • Optional flags that change the behavior, just like RewriteRule
    60. 60. RewriteCond variable pattern [flags]
    61. 61. RewriteCond variable pattern [flags] HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
    62. 62. RewriteCond variable pattern [flags] HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
    63. 63. RewriteCond variable pattern [flags] HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
    64. 64. RewriteCond variable pattern [flags] HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
    65. 65. RewriteCond variable pattern [flags] HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
    66. 66. RewriteCond variable pattern [flags] HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
    67. 67. RewriteCond variable pattern [flags] HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
    68. 68. RewriteCond variable pattern [flags] HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
    69. 69. Exhibit A, Explained (Again) # Default WordPress rewrite rules # Turn rewrite engine on RewriteEngine On # Base all rewrites on ‘/’ URL RewriteBase / # If the requested file does not exist RewriteCond %{REQUEST_FILENAME} !-f # ...or if no directory exists... RewriteCond %{REQUEST_FILENAME} !-d # Rewrite the URL to /index.php RewriteRule . /index.php [L]
    70. 70. non-www to www RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com$ [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] Pattern Flags Substitution
    71. 71. non-www to www RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com$ [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] Match this pattern Pattern inside parentheses is capture as variable ‘$1’
    72. 72. non-www to www RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com$ [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] Substitute it with “http://www.example.com”
    73. 73. non-www to www RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com$ [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] The pattern captured inside the parentheses
    74. 74. non-www to www RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com$ [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
    75. 75. non-www to www RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com$ [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] Redirect with a 301 Code
    76. 76. non-www to www RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com$ [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] Make this the last RewriteRule
    77. 77. RewriteOptions inherit
    78. 78. RewriteOptions inherit • Inherit the configuration of the parent
    79. 79. RewriteOptions inherit • Inherit the configuration of the parent • In per-virtual-server context, maps, conditions and rules of the main server are inherited
    80. 80. RewriteOptions inherit • Inherit the configuration of the parent • In per-virtual-server context, maps, conditions and rules of the main server are inherited • In per-directory context, conditions and rules of the parent directory's .htaccess configuration are inherited
    81. 81. Example
    82. 82. Example in httpd.conf
    83. 83. Example in httpd.conf ... RewriteRule ^(.*)$ index.php ... <VirtualHost 192.168.1.120:80> ServerName zomgbacon.com DocumentRoot /home/bacon/public_html # Turn on the rewrite engine and inherit any rules RewriteEngine On RewriteOptions Inherit </VirtualHost> ...
    84. 84. RewriteBase URL-path
    85. 85. RewriteBase URL-path • Sets the base URL for per-directory rewrites
    86. 86. RewriteBase URL-path • Sets the base URL for per-directory rewrites • URLs are NOT directly related to physical filename paths
    87. 87. RewriteBase Example
    88. 88. RewriteBase Example in .htaccess file
    89. 89. RewriteBase Example in .htaccess file # # /abc/def/.htaccess -- per-dir config file for directory /abc/def # Remember: /abc/def is the physical path of /xyz, i.e., the server # has a 'Alias /xyz /abc/def' directive e.g. # RewriteEngine On # let the server know that we were reached via /xyz and not # via the physical path prefix /abc/def RewriteBase /xyz # now the rewriting rules RewriteRule ^oldstuff.html$ newstuff.html
    90. 90. RewriteLog log-path
    91. 91. RewriteLog log-path • Logs rewrites
    92. 92. RewriteLog log-path • Logs rewrites • Level of logging can be tuned
    93. 93. RewriteLog log-path • Logs rewrites • Level of logging can be tuned • Relative paths are relative to DocumentRoot
    94. 94. RewriteLog log-path • Logs rewrites • Level of logging can be tuned • Relative paths are relative to DocumentRoot • Absolute paths are...well, absolute.
    95. 95. RewriteLogLevel level
    96. 96. RewriteLogLevel level • Integer value 0-9
    97. 97. RewriteLogLevel level • Integer value 0-9 • 0 == disabled
    98. 98. RewriteLogLevel level • Integer value 0-9 • 0 == disabled • 9 == log nearly everything
    99. 99. RewriteLogLevel level • Integer value 0-9 • 0 == disabled • 9 == log nearly everything • More verbose, greater impact on performance
    100. 100. RewriteLogLevel level • Integer value 0-9 • 0 == disabled • 9 == log nearly everything • More verbose, greater impact on performance • Higher levels == 2 or higher for debugging only
    101. 101. Logging Examples
    102. 102. Logging Examples in httpd.conf
    103. 103. Logging Examples in httpd.conf ... <VirtualHost 192.168.1.120:80> ServerName zomgbacon.com DocumentRoot /home/bacon/public_html # /home/bacon/public_html/rewrite.log RewriteLog rewrite.log # Make it semi-verbose RewriteLogLevel 5 # turn on rewrite engine and inherit rules RewriteEngine on RewriteOptions Inherit </VirtualHost> ...
    104. 104. fin

    ×