SlideShare a Scribd company logo
1 of 68
Download to read offline
mod_rewrite
    Introduction to mod_rewrite
Rich Bowen, Web Guy, Asbury College
        rbowen@apache.org

 http://people.apache.org/~rbowen/

       ApacheCon US, 2006




                 1
Outline
Regex basics

RewriteRule

RewriteCond

RewriteMap

The evils of .htaccess files

Assorted odds and ends



                    2
mod_rewrite
is not magic
 Fear, more than
 complexity, makes
 mod_rewrite difficult




                    3
Although, it is complex

            ``The great thing about
                 mod_rewrite
    is it gives you all the configurability
and flexibility of Sendmail. The downside to
     mod_rewrite is that it gives you all
     the configurability and flexibility of
                 Sendmail.''
              -- Brian Behlendorf




                     4
And let’s not forget
      voodoo!
  `` Despite the tons of
 examples and docs,
mod_rewrite is voodoo.
Damned cool voodoo,
     but still voodoo. ''
      -- Brian Moore




                   5
Line noise
  quot;Regular expressions
   are just line noise.
      I hate them!quot;
    (Heard 20 times per
      day on IRC)



          When you hear it often
          enough, you start to
          believe it

      6
Now that that’s out of the way


Regular expressions are not magic

They are an algebraic expression of text

patterns

Once you get over the mysticism, it can still be

hard, but it's no longer mysterious




                      7
Vocabulary


We’re going to start with a very small
vocabulary, and work up from there

Most of the time, this vocabulary is all that
you’ll need




                    8
.


. matches any character

“a.b” matches acb, axb, a@b, and so on

It also matches Decalb and Marbelized




                    9
+

+ means that something needs to appear one
or more times

“a+” matches a, aa, aaa, and
Stellaaaaaaaaaa!

The thing that is repeating isn’t necessarily
just a single character




                    10
*

* means that the previous thingy needs to
match zero or more times

This is subtly different from + and some
folks miss the distinction

“giraf*e” matches giraffe and girafe

It also matches girae




                    11
?

? means that the previous thingy needs to
match zero or one times

In other words, it makes it optional

“colou?r” matches color and colour




                    12
^
^ is called an anchor

It requires that the string start with the
pattern

^A matches ANDY but it does not match
CANDY

Pronounced “hat” or “caret” or “circumflex”
or “pointy-up thingy”



                    13
$

$ is the other anchor

It requires that the string end with the
pattern

a$ matches canada but not afghanistan




                    14
()

( ) allows you to group several characters
into one thingy

This allows you to apply repetition
characters (*, +, and ?) to a larger group of
characters.

“(ab)+” matches ababababababab




                    15
( ), continued
( ) allows you to capture a match so that you
can use it later.

The value of the matched bit is stored in a
variable called a backreference

It might be called $1 or %1 depending on the
context

The second match is called $2 (or %2) and so
on


                    16
[]

[ ] defines a “character class”

[abc] matches a or or b or c

“c[uoa]t” matches cut, cot, or cat

It also matches cote

It does not match coat




                     17
NOT

In mod_rewrite regular expressions, ! negates
any match

In a character class, ^ negates the character
class

[^ab] matches any character except for a or
b.




                    18
So, what does this have to do with
             Apache?
 mod_rewrite lets you match URLs (or other
 things) and transform the target of the URL
 based on that match.


  RewriteEngine On
  # Burninate ColdFusion!
  RewriteRule (.*).cfm$ $1.php [PT]
  # And there was much rejoicing. Yaaaay.




                       19
RewriteEngine
“RewriteEngine On” enables
the mod_rewrite rewriting
engine

No rewrite rules will be
performed unless this is
enabled in the active scope

It never hurts to say it again




                      20
RewriteLog
RewriteLog /www/logs/rewrite_log
RewriteLogLevel 9

You should turn on the RewriteLog before you
do any troubleshooting.




                     21
RewriteRule
 RewriteRule pattern target [flags]

The pattern part is the regular expression
that you want to look for in the URL

If they try to go HERE send them HERE
instead.

The behavior can be further modified by the
use of one or more flags




                     22
Example 1

SEO - “Search Engine Optimization”

Frequently based on misconceptions about
how search engines work

Typical strategy is to make “clean URLs” -
Avoid ?argument=value&xyz=123




                    23
URL beautification
  A URL looks like:
http://example.com/cgi-bin/book.cgi?author=bowen&topic=apache


We would prefer that it looked like

 http://example.com/book/bowen/apache

It’s easier to type, and easier to
             remember




                                24
Example 1, cont’d
RewriteRule ^/book/(.*)/(.*) 
  /cgi-bin/book.cgi?topic=$1&author=$2 [PT]

     User does not notice that the transformation
     has been made

     Used $1 and $2 to capture what was
     requested

     Slight oversimplification. Should probably use
     ([^/]+) instead.
                         25
Flags

Flags can modify the behavior of a
RewriteRule

I used a flag in the example, and didn’t tell
you what it meant

So, here are the flags




                    26
mod_rewrite
By the way ...
Default is to treat the rewrite target as a
file path

If the target starts in http:// or https://
then it is treated as a URL, and a [R] is
assumed (Redirect)

In a .htaccess file, or in <Directory> scope,
the file path is assumed to be relative to
that scope


                    28
RewriteRule flags
[Flag] appears at end of RewriteRule

More than one flag separated by commas

I recommend using flags even when the
default is what you want - it makes it easier
to read later

Each flag has a longer form, which you can
use for greater readability.

There’s *lots* of flags


                    29
Chain


[C] or [Chain]

Rules are considered as a whole. If one fails,
the entire chain is abandoned




                    30
Cookie
         [CO=NAME:Value:Domain[:lifetime[:path]]

         Long form [cookie=...]

         Sets a cookie

RewriteRule ^/index.html - [CO=frontdoor:yes:.example.com]



       In this case, the default values for
       path (”/”) and lifetime (”session”)
       are assumed.

                               31
Env
   [E=var:val]

   Long form [env=...]

   Sets environment variable

   Note that most of the time, SetEnvIf works
   just fine

RewriteRule .jpg$ - [env=dontlog:1]



                         32
Forbidden
   [F] or [Forbidden] forces a 403 Forbidden
   response

   Consider mod_security instead for pattern-
   based URL blocking
  RewriteEngine On
  RewriteRule (cmd|root).exe - [F]
  You could use this in conjunction with [E]
  to avoid logging that stuff
RewriteRule (cmd|root).exe - [F,E=dontlog:1]
CustomLog /var/log/apache/access_log combined 
        env=!dontlog

                        33
Handler
[H=application/x-httpd-php]

Forces the use of a particular handler to
handle the resulting URL

Can often be replaced with using [PT] but is
quite a bit faster

Available in Apache 2.2



                    34
Last
[L] indicates that you’ve reached the end of
the current ruleset

Any rules following this will be considered as
a completely new ruleset

It’s a good idea to use it, even when it would
otherwise be default behavior. It helps make
rulesets more readable.



                    35
Next
The [N] or [Next] flag is a good way to get
yourself into an infinite loop

It tells mod_rewrite to run the entire
ruleset again from the beginning

Can be useful for doing “global search and
replace” stuff

I find RewriteMap much more useful in those
situations

                   36
NoCase


[NC] or [nocase] makes the RewriteRule case
insensitive

Regular expressions are case-sensitive by
default




                   37
NoEscape

[NE] or [noescape] disables the default
behavior of escaping (url-encoding) special
characters like #, ?, and so on

Useful for redirecting to a page #anchor




                    38
NoSubreq

[NS] or [nosubreq] ensures that the rule
won’t run on subrequests

Subrequests are things like SSI evaluations

Image and css requests are NOT subrequests




                    39
Proxy
  [P] rules are served through a proxy
  subrequest

  mod_proxy must be installed for this flag to
  work
RewriteEngine On
RewriteRule (.*).(jpg|gif|png) 
 http://images.example.com$1.$2 [P]




                     40
Passthrough

[PT] or [passthrough]

Hands it back to the URL mapping phase

Treat this as though this was the original
request




                    41
QSAppend



[QSA] or [qsappend] appends to the query
string, rather than replacing it.




                   42
Redirect

[R] or [redirect] forces a 302 Redirect

Note that in this case, the user will see the
new URL in their browser

This is the default behavior when the target
starts with http:// or https://




                    43
Skip

    [S=n] or [skip=n] skips the next n
    RewriteRules

    This is very weird

    I’ve never used this in the real world. Could
    be used as a sort of inverse RewriteCond
    (viz WordPress)
RewriteRule %{REQUEST_FILENAME} -f [S=15]


                         44
Type
        [T=text/html]

        Forces the Mime type on the resulting URL

        Used to do this instead of [H] in some
        contexts

        Good to ensure that file-path redirects are
        handled correctly

RewriteRule ^(.+.php)s$ $1 [T=application/x-httpd-php-source]



                               45
RewriteCond
  Causes a rewrite to be conditional

  Can check the value of any variable and
  make the rewrite conditional on that.
RewriteCond TestString Pattern [Flags]




                     46
RewriteCond

The test string can be just about anything

Env vars, headers, or a literal string

Backreferences become %1, %2, etc




                     47
Looping
    Looping occurs when the target of a rewrite
    rule matches the pattern

    This results in an infinite loop of rewrites


RewriteCond %{REQUEST_URI} 
       !^/example.html
RewriteRule ^/example /example.html [PT]




                        48
Conditional rewrites
    Rewrites conditional on some arbitrary
    thingy

    Only first Rule is dependent



RewriteEngine   on
RewriteCond     %{TIME_HOUR}%{TIME_MIN} >0700
RewriteCond     %{TIME_HOUR}%{TIME_MIN} <1900
RewriteRule     ^page.html$    page.day.html
RewriteRule     ^page.html$    page.night.html



                         49
SSL Rewrites
   Redirect requests to https:// if the request
   was for http

   (In a .htaccess file)




RewriteCond %{HTTPS} !ON
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R]




                          50
RewriteMap

Call an external program, or map file, to
perform the rewrite

Useful for very complex rewrites, or perhaps
ones that rely on something outside of
Apache




                   51
RewriteMap - file
     File of one-to-one relationships

RewriteMap docsmap txt:/www/conf/docsmap.txt
RewriteRule ^/docs/(.*) ${docsmap:$1} [R,NE]


   Where docsmap.txt contains:

Alias http://httpd.apache.org/docs/mod_alias.html#alias
Redirect http://httpd.apache.org/docs/mod_alias.html#redirect
... etc



Requests for http://example.com/docs/something
now get redirected to the Apache docs site for
‘something’. [NE] makes the #anchor bit work.

                               52
Poor-man’s load balancing
        Random selection of server for “load
        balancing”

    RewriteMap servers rnd:/www/conf/servers.txt
    RewriteRule (.*) http://${servers:loadbalance}$1 [P,NS]


                 servers.txt contains:

     loadbalance mars|jupiter|saturn|neptune


Requests are now randomly distributed between the four
servers. The ‘NS’ ensures that the proxied URL doesn’t
get re-rewritten.
                              53
dbm
RewriteMap asbury 
       dbm:/usr/local/apache/conf/aliases.map

       Convert a one-to-one text mapping to a dbm
       file

       httxt2dbm utility (2.0)




                           54
RewriteMap - program
   Call an external program to do the rewrite

   Perl is a common choice here, due to its skill
   at handling text.

RewriteMap dash2score 
   prg:/usr/local/apache/conf/dash2score.pl
RewriteEngine On
RewriteRule (.*-.*) ${dash2score:$1} [PT]




                       55
dash2score.pl
 #!/usr/bin/perl
 $| = 1; # Turn off buffering
 while (<STDIN>) {
         s/-/_/g; # Replace - with _ globally
         print $_;
 }

Turning off buffering is necessary
because we need the output
immediately for each line we feed it.

Apache starts the script on server
startup, and keeps it running for the
life of the server process
SQL (in 2.3-HEAD)


Just committed on Monday

Have a SQL statement in the RewriteMap
directive which returns the mapping




                  57
.htaccess files

.htaccess files are evil

However, a lot of people have no choice

So ...




                    58
.htaccess files
In .htaccess files, or <Directory> scope,
everything is assumed to be relative to that
current scope

So, that scope is removed from the
RewriteRule

^/index.html in httpd.conf becomes
^index.html in a .htaccess file or
<Directory> scope


                   59
.htaccess files
RewriteLog is particularly useful when trying
to get .htaccess file RewriteRules working.

However, you can’t turn on RewriteLog in
a .htaccess file, and presumably you’re
using .htaccess files because you don’t have
access to the main server config.

It’s a good idea to set up a test server on
your home PC and test there with
RewriteLog enabled

                    60
.htaccess files
The rewrite pattern is relative to the
current directory

The rewrite target is also relative to the
current directory

In httpd.conf, the rewrite target is assumed
to be a file path. In .htaccess files, that file
path is relative to the current directory, so it
seems to be a URI redirect.


                     61
Further resources

http://rewrite.drbacchus.com/

“Definitive Guide to mod_rewrite” by Rich
Bowen, from APress

http://httpd.apache.org/docs-2.2/rewrite/




                   62
Questions?




    63
Bonus slides - Recipes



Redirect everything to a central handler




                   64
RewriteEngine On
RewriteCond %{REQUEST_URI} !handler.php
RewriteRule (.*) /handler.php?$1 [PT,L,NE]


     All requests are sent to handler.php
  The request is passed as a QUERY_STRING
argument to handler.php so that it knows what
                was requested.

                      65
Virtual Hosts



Rewrite a request to a directory based on
the requested hostname.




                   66
RewriteEngine On
RewriteCond %{HTTP_HOST} (.*).example.com [NC]
RewriteRule (.*) /home/%1/www$1

       The hostname ends up in %1

       The requested path is in $1 - includes leading
       slash

       Will probably have to do special things for
       handlers (like .php files)

                           67
.phps source handler
RewriteRule (.*).phps 
  $1.php [H=application/x-httpd-php-source]



                           Syntax-highlighted code
                           rendering of any .php
                           file




                      68

More Related Content

What's hot

Tips
TipsTips
Tipsmclee
 
The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)Ki Sung Bae
 
Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Alena Holligan
 
groovy rules
groovy rulesgroovy rules
groovy rulesPaul King
 
Creating "Secure" PHP Applications, Part 1, Explicit Code & QA
Creating "Secure" PHP Applications, Part 1, Explicit Code & QACreating "Secure" PHP Applications, Part 1, Explicit Code & QA
Creating "Secure" PHP Applications, Part 1, Explicit Code & QAarchwisp
 
Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4Jeff Carouth
 
Pursuing the Strong, Not So Silent Type: A Haskell Story
Pursuing the Strong, Not So Silent Type: A Haskell StoryPursuing the Strong, Not So Silent Type: A Haskell Story
Pursuing the Strong, Not So Silent Type: A Haskell StoryKatie Ots
 
Haskell is Not For Production and Other Tales
Haskell is Not For Production and Other TalesHaskell is Not For Production and Other Tales
Haskell is Not For Production and Other TalesKatie Ots
 
DashProfiler 200807
DashProfiler 200807DashProfiler 200807
DashProfiler 200807Tim Bunce
 
Groovy Domain Specific Languages - SpringOne2GX 2012
Groovy Domain Specific Languages - SpringOne2GX 2012Groovy Domain Specific Languages - SpringOne2GX 2012
Groovy Domain Specific Languages - SpringOne2GX 2012Guillaume Laforge
 
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)James Titcumb
 
Bringing modern PHP development to IBM i (ZendCon 2016)
Bringing modern PHP development to IBM i (ZendCon 2016)Bringing modern PHP development to IBM i (ZendCon 2016)
Bringing modern PHP development to IBM i (ZendCon 2016)James Titcumb
 
Metaprogramming and Folly
Metaprogramming and FollyMetaprogramming and Folly
Metaprogramming and FollyHaseeb Qureshi
 

What's hot (19)

Tips
TipsTips
Tips
 
The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)
 
Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016
 
ruby1_6up
ruby1_6upruby1_6up
ruby1_6up
 
Apache Hacks
Apache HacksApache Hacks
Apache Hacks
 
groovy rules
groovy rulesgroovy rules
groovy rules
 
Creating "Secure" PHP Applications, Part 1, Explicit Code & QA
Creating "Secure" PHP Applications, Part 1, Explicit Code & QACreating "Secure" PHP Applications, Part 1, Explicit Code & QA
Creating "Secure" PHP Applications, Part 1, Explicit Code & QA
 
Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4
 
Python Programming Essentials - M8 - String Methods
Python Programming Essentials - M8 - String MethodsPython Programming Essentials - M8 - String Methods
Python Programming Essentials - M8 - String Methods
 
Pursuing the Strong, Not So Silent Type: A Haskell Story
Pursuing the Strong, Not So Silent Type: A Haskell StoryPursuing the Strong, Not So Silent Type: A Haskell Story
Pursuing the Strong, Not So Silent Type: A Haskell Story
 
Haskell is Not For Production and Other Tales
Haskell is Not For Production and Other TalesHaskell is Not For Production and Other Tales
Haskell is Not For Production and Other Tales
 
DashProfiler 200807
DashProfiler 200807DashProfiler 200807
DashProfiler 200807
 
What's New in ZF 1.10
What's New in ZF 1.10What's New in ZF 1.10
What's New in ZF 1.10
 
Spock
SpockSpock
Spock
 
Groovy Domain Specific Languages - SpringOne2GX 2012
Groovy Domain Specific Languages - SpringOne2GX 2012Groovy Domain Specific Languages - SpringOne2GX 2012
Groovy Domain Specific Languages - SpringOne2GX 2012
 
DEBUGGING FUZZY XPATH QUERIES
DEBUGGING FUZZY XPATH QUERIESDEBUGGING FUZZY XPATH QUERIES
DEBUGGING FUZZY XPATH QUERIES
 
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
 
Bringing modern PHP development to IBM i (ZendCon 2016)
Bringing modern PHP development to IBM i (ZendCon 2016)Bringing modern PHP development to IBM i (ZendCon 2016)
Bringing modern PHP development to IBM i (ZendCon 2016)
 
Metaprogramming and Folly
Metaprogramming and FollyMetaprogramming and Folly
Metaprogramming and Folly
 

Similar to mod_rewrite

RubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - KeynoteRubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - KeynoteDr Nic Williams
 
URL Mapping, with and without mod_rewrite
URL Mapping, with and without mod_rewriteURL Mapping, with and without mod_rewrite
URL Mapping, with and without mod_rewriteRich Bowen
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl TechniquesDave Cross
 
Apache - Mod-Rewrite
Apache - Mod-RewriteApache - Mod-Rewrite
Apache - Mod-RewriteMarakana Inc.
 
RSpec 3: The new, the old, the good
RSpec 3: The new, the old, the goodRSpec 3: The new, the old, the good
RSpec 3: The new, the old, the goodmglrnm
 
In-Depth Guide On WordPress Coding Standards For PHP & HTML
In-Depth Guide On WordPress Coding Standards For PHP & HTMLIn-Depth Guide On WordPress Coding Standards For PHP & HTML
In-Depth Guide On WordPress Coding Standards For PHP & HTMLeSparkBiz
 
Beijing Perl Workshop 2008 Hiveminder Secret Sauce
Beijing Perl Workshop 2008 Hiveminder Secret SauceBeijing Perl Workshop 2008 Hiveminder Secret Sauce
Beijing Perl Workshop 2008 Hiveminder Secret SauceJesse Vincent
 
Hiveminder - Everything but the Secret Sauce
Hiveminder - Everything but the Secret SauceHiveminder - Everything but the Secret Sauce
Hiveminder - Everything but the Secret SauceJesse Vincent
 
PSGI and Plack from first principles
PSGI and Plack from first principlesPSGI and Plack from first principles
PSGI and Plack from first principlesPerl Careers
 
Real life-coffeescript
Real life-coffeescriptReal life-coffeescript
Real life-coffeescriptDavid Furber
 
Angular JS in 2017
Angular JS in 2017Angular JS in 2017
Angular JS in 2017Ayush Sharma
 
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasmineSingle Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasminePaulo Ragonha
 
Perly Parsing with Regexp::Grammars
Perly Parsing with Regexp::GrammarsPerly Parsing with Regexp::Grammars
Perly Parsing with Regexp::GrammarsWorkhorse Computing
 
[Bristol WordPress] Supercharging WordPress Development
[Bristol WordPress] Supercharging WordPress Development[Bristol WordPress] Supercharging WordPress Development
[Bristol WordPress] Supercharging WordPress DevelopmentAdam Tomat
 
Catalyst - refactor large apps with it and have fun!
Catalyst - refactor large apps with it and have fun!Catalyst - refactor large apps with it and have fun!
Catalyst - refactor large apps with it and have fun!mold
 
Apache Rewrite Rules
Apache Rewrite RulesApache Rewrite Rules
Apache Rewrite RulesAkhil Bansal
 
Coder Presentation Szeged
Coder Presentation SzegedCoder Presentation Szeged
Coder Presentation SzegedDoug Green
 

Similar to mod_rewrite (20)

mod_rewrite
mod_rewritemod_rewrite
mod_rewrite
 
RubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - KeynoteRubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - Keynote
 
URL Mapping, with and without mod_rewrite
URL Mapping, with and without mod_rewriteURL Mapping, with and without mod_rewrite
URL Mapping, with and without mod_rewrite
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
 
Apache - Mod-Rewrite
Apache - Mod-RewriteApache - Mod-Rewrite
Apache - Mod-Rewrite
 
RSpec 3: The new, the old, the good
RSpec 3: The new, the old, the goodRSpec 3: The new, the old, the good
RSpec 3: The new, the old, the good
 
In-Depth Guide On WordPress Coding Standards For PHP & HTML
In-Depth Guide On WordPress Coding Standards For PHP & HTMLIn-Depth Guide On WordPress Coding Standards For PHP & HTML
In-Depth Guide On WordPress Coding Standards For PHP & HTML
 
Beijing Perl Workshop 2008 Hiveminder Secret Sauce
Beijing Perl Workshop 2008 Hiveminder Secret SauceBeijing Perl Workshop 2008 Hiveminder Secret Sauce
Beijing Perl Workshop 2008 Hiveminder Secret Sauce
 
Hiveminder - Everything but the Secret Sauce
Hiveminder - Everything but the Secret SauceHiveminder - Everything but the Secret Sauce
Hiveminder - Everything but the Secret Sauce
 
PSGI and Plack from first principles
PSGI and Plack from first principlesPSGI and Plack from first principles
PSGI and Plack from first principles
 
Real life-coffeescript
Real life-coffeescriptReal life-coffeescript
Real life-coffeescript
 
Angular JS in 2017
Angular JS in 2017Angular JS in 2017
Angular JS in 2017
 
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasmineSingle Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
 
Perly Parsing with Regexp::Grammars
Perly Parsing with Regexp::GrammarsPerly Parsing with Regexp::Grammars
Perly Parsing with Regexp::Grammars
 
[Bristol WordPress] Supercharging WordPress Development
[Bristol WordPress] Supercharging WordPress Development[Bristol WordPress] Supercharging WordPress Development
[Bristol WordPress] Supercharging WordPress Development
 
Catalyst - refactor large apps with it and have fun!
Catalyst - refactor large apps with it and have fun!Catalyst - refactor large apps with it and have fun!
Catalyst - refactor large apps with it and have fun!
 
Ruby on Rails Presentation
Ruby on Rails PresentationRuby on Rails Presentation
Ruby on Rails Presentation
 
Apache Rewrite Rules
Apache Rewrite RulesApache Rewrite Rules
Apache Rewrite Rules
 
Ruby on Rails
Ruby on RailsRuby on Rails
Ruby on Rails
 
Coder Presentation Szeged
Coder Presentation SzegedCoder Presentation Szeged
Coder Presentation Szeged
 

More from elliando dias

Clojurescript slides
Clojurescript slidesClojurescript slides
Clojurescript slideselliando dias
 
Why you should be excited about ClojureScript
Why you should be excited about ClojureScriptWhy you should be excited about ClojureScript
Why you should be excited about ClojureScriptelliando dias
 
Functional Programming with Immutable Data Structures
Functional Programming with Immutable Data StructuresFunctional Programming with Immutable Data Structures
Functional Programming with Immutable Data Structureselliando dias
 
Nomenclatura e peças de container
Nomenclatura  e peças de containerNomenclatura  e peças de container
Nomenclatura e peças de containerelliando dias
 
Polyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better AgilityPolyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better Agilityelliando dias
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Librarieselliando dias
 
How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!elliando dias
 
A Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the WebA Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the Webelliando dias
 
Introdução ao Arduino
Introdução ao ArduinoIntrodução ao Arduino
Introdução ao Arduinoelliando dias
 
Incanter Data Sorcery
Incanter Data SorceryIncanter Data Sorcery
Incanter Data Sorceryelliando dias
 
Fab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine DesignFab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine Designelliando dias
 
The Digital Revolution: Machines that makes
The Digital Revolution: Machines that makesThe Digital Revolution: Machines that makes
The Digital Revolution: Machines that makeselliando dias
 
Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.elliando dias
 
Hadoop and Hive Development at Facebook
Hadoop and Hive Development at FacebookHadoop and Hive Development at Facebook
Hadoop and Hive Development at Facebookelliando dias
 
Multi-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case StudyMulti-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case Studyelliando dias
 

More from elliando dias (20)

Clojurescript slides
Clojurescript slidesClojurescript slides
Clojurescript slides
 
Why you should be excited about ClojureScript
Why you should be excited about ClojureScriptWhy you should be excited about ClojureScript
Why you should be excited about ClojureScript
 
Functional Programming with Immutable Data Structures
Functional Programming with Immutable Data StructuresFunctional Programming with Immutable Data Structures
Functional Programming with Immutable Data Structures
 
Nomenclatura e peças de container
Nomenclatura  e peças de containerNomenclatura  e peças de container
Nomenclatura e peças de container
 
Geometria Projetiva
Geometria ProjetivaGeometria Projetiva
Geometria Projetiva
 
Polyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better AgilityPolyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better Agility
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Libraries
 
How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!
 
Ragel talk
Ragel talkRagel talk
Ragel talk
 
A Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the WebA Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the Web
 
Introdução ao Arduino
Introdução ao ArduinoIntrodução ao Arduino
Introdução ao Arduino
 
Minicurso arduino
Minicurso arduinoMinicurso arduino
Minicurso arduino
 
Incanter Data Sorcery
Incanter Data SorceryIncanter Data Sorcery
Incanter Data Sorcery
 
Rango
RangoRango
Rango
 
Fab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine DesignFab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine Design
 
The Digital Revolution: Machines that makes
The Digital Revolution: Machines that makesThe Digital Revolution: Machines that makes
The Digital Revolution: Machines that makes
 
Hadoop + Clojure
Hadoop + ClojureHadoop + Clojure
Hadoop + Clojure
 
Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.
 
Hadoop and Hive Development at Facebook
Hadoop and Hive Development at FacebookHadoop and Hive Development at Facebook
Hadoop and Hive Development at Facebook
 
Multi-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case StudyMulti-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case Study
 

Recently uploaded

My key hands-on projects in Quantum, and QAI
My key hands-on projects in Quantum, and QAIMy key hands-on projects in Quantum, and QAI
My key hands-on projects in Quantum, and QAIVijayananda Mohire
 
Trailblazer Community - Flows Workshop (Session 2)
Trailblazer Community - Flows Workshop (Session 2)Trailblazer Community - Flows Workshop (Session 2)
Trailblazer Community - Flows Workshop (Session 2)Muhammad Tiham Siddiqui
 
AI Workshops at Computers In Libraries 2024
AI Workshops at Computers In Libraries 2024AI Workshops at Computers In Libraries 2024
AI Workshops at Computers In Libraries 2024Brian Pichman
 
Graphene Quantum Dots-Based Composites for Biomedical Applications
Graphene Quantum Dots-Based Composites for  Biomedical ApplicationsGraphene Quantum Dots-Based Composites for  Biomedical Applications
Graphene Quantum Dots-Based Composites for Biomedical Applicationsnooralam814309
 
Keep Your Finger on the Pulse of Your Building's Performance with IES Live
Keep Your Finger on the Pulse of Your Building's Performance with IES LiveKeep Your Finger on the Pulse of Your Building's Performance with IES Live
Keep Your Finger on the Pulse of Your Building's Performance with IES LiveIES VE
 
UiPath Studio Web workshop Series - Day 3
UiPath Studio Web workshop Series - Day 3UiPath Studio Web workshop Series - Day 3
UiPath Studio Web workshop Series - Day 3DianaGray10
 
Emil Eifrem at GraphSummit Copenhagen 2024 - The Art of the Possible.pptx
Emil Eifrem at GraphSummit Copenhagen 2024 - The Art of the Possible.pptxEmil Eifrem at GraphSummit Copenhagen 2024 - The Art of the Possible.pptx
Emil Eifrem at GraphSummit Copenhagen 2024 - The Art of the Possible.pptxNeo4j
 
UiPath Studio Web workshop series - Day 2
UiPath Studio Web workshop series - Day 2UiPath Studio Web workshop series - Day 2
UiPath Studio Web workshop series - Day 2DianaGray10
 
Introduction - IPLOOK NETWORKS CO., LTD.
Introduction - IPLOOK NETWORKS CO., LTD.Introduction - IPLOOK NETWORKS CO., LTD.
Introduction - IPLOOK NETWORKS CO., LTD.IPLOOK Networks
 
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - Tech
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - TechWebinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - Tech
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - TechProduct School
 
Scenario Library et REX Discover industry- and role- based scenarios
Scenario Library et REX Discover industry- and role- based scenariosScenario Library et REX Discover industry- and role- based scenarios
Scenario Library et REX Discover industry- and role- based scenariosErol GIRAUDY
 
Explore the UiPath Community and ways you can benefit on your journey to auto...
Explore the UiPath Community and ways you can benefit on your journey to auto...Explore the UiPath Community and ways you can benefit on your journey to auto...
Explore the UiPath Community and ways you can benefit on your journey to auto...DianaGray10
 
Where developers are challenged, what developers want and where DevEx is going
Where developers are challenged, what developers want and where DevEx is goingWhere developers are challenged, what developers want and where DevEx is going
Where developers are challenged, what developers want and where DevEx is goingFrancesco Corti
 
UiPath Studio Web workshop series - Day 1
UiPath Studio Web workshop series  - Day 1UiPath Studio Web workshop series  - Day 1
UiPath Studio Web workshop series - Day 1DianaGray10
 
3 Pitfalls Everyone Should Avoid with Cloud Data
3 Pitfalls Everyone Should Avoid with Cloud Data3 Pitfalls Everyone Should Avoid with Cloud Data
3 Pitfalls Everyone Should Avoid with Cloud DataEric D. Schabell
 
Patch notes explaining DISARM Version 1.4 update
Patch notes explaining DISARM Version 1.4 updatePatch notes explaining DISARM Version 1.4 update
Patch notes explaining DISARM Version 1.4 updateadam112203
 
Novo Nordisk's journey in developing an open-source application on Neo4j
Novo Nordisk's journey in developing an open-source application on Neo4jNovo Nordisk's journey in developing an open-source application on Neo4j
Novo Nordisk's journey in developing an open-source application on Neo4jNeo4j
 
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedInOutage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedInThousandEyes
 
March Patch Tuesday
March Patch TuesdayMarch Patch Tuesday
March Patch TuesdayIvanti
 

Recently uploaded (20)

My key hands-on projects in Quantum, and QAI
My key hands-on projects in Quantum, and QAIMy key hands-on projects in Quantum, and QAI
My key hands-on projects in Quantum, and QAI
 
Trailblazer Community - Flows Workshop (Session 2)
Trailblazer Community - Flows Workshop (Session 2)Trailblazer Community - Flows Workshop (Session 2)
Trailblazer Community - Flows Workshop (Session 2)
 
SheDev 2024
SheDev 2024SheDev 2024
SheDev 2024
 
AI Workshops at Computers In Libraries 2024
AI Workshops at Computers In Libraries 2024AI Workshops at Computers In Libraries 2024
AI Workshops at Computers In Libraries 2024
 
Graphene Quantum Dots-Based Composites for Biomedical Applications
Graphene Quantum Dots-Based Composites for  Biomedical ApplicationsGraphene Quantum Dots-Based Composites for  Biomedical Applications
Graphene Quantum Dots-Based Composites for Biomedical Applications
 
Keep Your Finger on the Pulse of Your Building's Performance with IES Live
Keep Your Finger on the Pulse of Your Building's Performance with IES LiveKeep Your Finger on the Pulse of Your Building's Performance with IES Live
Keep Your Finger on the Pulse of Your Building's Performance with IES Live
 
UiPath Studio Web workshop Series - Day 3
UiPath Studio Web workshop Series - Day 3UiPath Studio Web workshop Series - Day 3
UiPath Studio Web workshop Series - Day 3
 
Emil Eifrem at GraphSummit Copenhagen 2024 - The Art of the Possible.pptx
Emil Eifrem at GraphSummit Copenhagen 2024 - The Art of the Possible.pptxEmil Eifrem at GraphSummit Copenhagen 2024 - The Art of the Possible.pptx
Emil Eifrem at GraphSummit Copenhagen 2024 - The Art of the Possible.pptx
 
UiPath Studio Web workshop series - Day 2
UiPath Studio Web workshop series - Day 2UiPath Studio Web workshop series - Day 2
UiPath Studio Web workshop series - Day 2
 
Introduction - IPLOOK NETWORKS CO., LTD.
Introduction - IPLOOK NETWORKS CO., LTD.Introduction - IPLOOK NETWORKS CO., LTD.
Introduction - IPLOOK NETWORKS CO., LTD.
 
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - Tech
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - TechWebinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - Tech
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - Tech
 
Scenario Library et REX Discover industry- and role- based scenarios
Scenario Library et REX Discover industry- and role- based scenariosScenario Library et REX Discover industry- and role- based scenarios
Scenario Library et REX Discover industry- and role- based scenarios
 
Explore the UiPath Community and ways you can benefit on your journey to auto...
Explore the UiPath Community and ways you can benefit on your journey to auto...Explore the UiPath Community and ways you can benefit on your journey to auto...
Explore the UiPath Community and ways you can benefit on your journey to auto...
 
Where developers are challenged, what developers want and where DevEx is going
Where developers are challenged, what developers want and where DevEx is goingWhere developers are challenged, what developers want and where DevEx is going
Where developers are challenged, what developers want and where DevEx is going
 
UiPath Studio Web workshop series - Day 1
UiPath Studio Web workshop series  - Day 1UiPath Studio Web workshop series  - Day 1
UiPath Studio Web workshop series - Day 1
 
3 Pitfalls Everyone Should Avoid with Cloud Data
3 Pitfalls Everyone Should Avoid with Cloud Data3 Pitfalls Everyone Should Avoid with Cloud Data
3 Pitfalls Everyone Should Avoid with Cloud Data
 
Patch notes explaining DISARM Version 1.4 update
Patch notes explaining DISARM Version 1.4 updatePatch notes explaining DISARM Version 1.4 update
Patch notes explaining DISARM Version 1.4 update
 
Novo Nordisk's journey in developing an open-source application on Neo4j
Novo Nordisk's journey in developing an open-source application on Neo4jNovo Nordisk's journey in developing an open-source application on Neo4j
Novo Nordisk's journey in developing an open-source application on Neo4j
 
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedInOutage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
 
March Patch Tuesday
March Patch TuesdayMarch Patch Tuesday
March Patch Tuesday
 

mod_rewrite

  • 1. mod_rewrite Introduction to mod_rewrite Rich Bowen, Web Guy, Asbury College rbowen@apache.org http://people.apache.org/~rbowen/ ApacheCon US, 2006 1
  • 2. Outline Regex basics RewriteRule RewriteCond RewriteMap The evils of .htaccess files Assorted odds and ends 2
  • 3. mod_rewrite is not magic Fear, more than complexity, makes mod_rewrite difficult 3
  • 4. Although, it is complex ``The great thing about mod_rewrite is it gives you all the configurability and flexibility of Sendmail. The downside to mod_rewrite is that it gives you all the configurability and flexibility of Sendmail.'' -- Brian Behlendorf 4
  • 5. And let’s not forget voodoo! `` Despite the tons of examples and docs, mod_rewrite is voodoo. Damned cool voodoo, but still voodoo. '' -- Brian Moore 5
  • 6. Line noise quot;Regular expressions are just line noise. I hate them!quot; (Heard 20 times per day on IRC) When you hear it often enough, you start to believe it 6
  • 7. Now that that’s out of the way Regular expressions are not magic They are an algebraic expression of text patterns Once you get over the mysticism, it can still be hard, but it's no longer mysterious 7
  • 8. Vocabulary We’re going to start with a very small vocabulary, and work up from there Most of the time, this vocabulary is all that you’ll need 8
  • 9. . . matches any character “a.b” matches acb, axb, a@b, and so on It also matches Decalb and Marbelized 9
  • 10. + + means that something needs to appear one or more times “a+” matches a, aa, aaa, and Stellaaaaaaaaaa! The thing that is repeating isn’t necessarily just a single character 10
  • 11. * * means that the previous thingy needs to match zero or more times This is subtly different from + and some folks miss the distinction “giraf*e” matches giraffe and girafe It also matches girae 11
  • 12. ? ? means that the previous thingy needs to match zero or one times In other words, it makes it optional “colou?r” matches color and colour 12
  • 13. ^ ^ is called an anchor It requires that the string start with the pattern ^A matches ANDY but it does not match CANDY Pronounced “hat” or “caret” or “circumflex” or “pointy-up thingy” 13
  • 14. $ $ is the other anchor It requires that the string end with the pattern a$ matches canada but not afghanistan 14
  • 15. () ( ) allows you to group several characters into one thingy This allows you to apply repetition characters (*, +, and ?) to a larger group of characters. “(ab)+” matches ababababababab 15
  • 16. ( ), continued ( ) allows you to capture a match so that you can use it later. The value of the matched bit is stored in a variable called a backreference It might be called $1 or %1 depending on the context The second match is called $2 (or %2) and so on 16
  • 17. [] [ ] defines a “character class” [abc] matches a or or b or c “c[uoa]t” matches cut, cot, or cat It also matches cote It does not match coat 17
  • 18. NOT In mod_rewrite regular expressions, ! negates any match In a character class, ^ negates the character class [^ab] matches any character except for a or b. 18
  • 19. So, what does this have to do with Apache? mod_rewrite lets you match URLs (or other things) and transform the target of the URL based on that match. RewriteEngine On # Burninate ColdFusion! RewriteRule (.*).cfm$ $1.php [PT] # And there was much rejoicing. Yaaaay. 19
  • 20. RewriteEngine “RewriteEngine On” enables the mod_rewrite rewriting engine No rewrite rules will be performed unless this is enabled in the active scope It never hurts to say it again 20
  • 21. RewriteLog RewriteLog /www/logs/rewrite_log RewriteLogLevel 9 You should turn on the RewriteLog before you do any troubleshooting. 21
  • 22. RewriteRule RewriteRule pattern target [flags] The pattern part is the regular expression that you want to look for in the URL If they try to go HERE send them HERE instead. The behavior can be further modified by the use of one or more flags 22
  • 23. Example 1 SEO - “Search Engine Optimization” Frequently based on misconceptions about how search engines work Typical strategy is to make “clean URLs” - Avoid ?argument=value&xyz=123 23
  • 24. URL beautification A URL looks like: http://example.com/cgi-bin/book.cgi?author=bowen&topic=apache We would prefer that it looked like http://example.com/book/bowen/apache It’s easier to type, and easier to remember 24
  • 25. Example 1, cont’d RewriteRule ^/book/(.*)/(.*) /cgi-bin/book.cgi?topic=$1&author=$2 [PT] User does not notice that the transformation has been made Used $1 and $2 to capture what was requested Slight oversimplification. Should probably use ([^/]+) instead. 25
  • 26. Flags Flags can modify the behavior of a RewriteRule I used a flag in the example, and didn’t tell you what it meant So, here are the flags 26
  • 28. By the way ... Default is to treat the rewrite target as a file path If the target starts in http:// or https:// then it is treated as a URL, and a [R] is assumed (Redirect) In a .htaccess file, or in <Directory> scope, the file path is assumed to be relative to that scope 28
  • 29. RewriteRule flags [Flag] appears at end of RewriteRule More than one flag separated by commas I recommend using flags even when the default is what you want - it makes it easier to read later Each flag has a longer form, which you can use for greater readability. There’s *lots* of flags 29
  • 30. Chain [C] or [Chain] Rules are considered as a whole. If one fails, the entire chain is abandoned 30
  • 31. Cookie [CO=NAME:Value:Domain[:lifetime[:path]] Long form [cookie=...] Sets a cookie RewriteRule ^/index.html - [CO=frontdoor:yes:.example.com] In this case, the default values for path (”/”) and lifetime (”session”) are assumed. 31
  • 32. Env [E=var:val] Long form [env=...] Sets environment variable Note that most of the time, SetEnvIf works just fine RewriteRule .jpg$ - [env=dontlog:1] 32
  • 33. Forbidden [F] or [Forbidden] forces a 403 Forbidden response Consider mod_security instead for pattern- based URL blocking RewriteEngine On RewriteRule (cmd|root).exe - [F] You could use this in conjunction with [E] to avoid logging that stuff RewriteRule (cmd|root).exe - [F,E=dontlog:1] CustomLog /var/log/apache/access_log combined env=!dontlog 33
  • 34. Handler [H=application/x-httpd-php] Forces the use of a particular handler to handle the resulting URL Can often be replaced with using [PT] but is quite a bit faster Available in Apache 2.2 34
  • 35. Last [L] indicates that you’ve reached the end of the current ruleset Any rules following this will be considered as a completely new ruleset It’s a good idea to use it, even when it would otherwise be default behavior. It helps make rulesets more readable. 35
  • 36. Next The [N] or [Next] flag is a good way to get yourself into an infinite loop It tells mod_rewrite to run the entire ruleset again from the beginning Can be useful for doing “global search and replace” stuff I find RewriteMap much more useful in those situations 36
  • 37. NoCase [NC] or [nocase] makes the RewriteRule case insensitive Regular expressions are case-sensitive by default 37
  • 38. NoEscape [NE] or [noescape] disables the default behavior of escaping (url-encoding) special characters like #, ?, and so on Useful for redirecting to a page #anchor 38
  • 39. NoSubreq [NS] or [nosubreq] ensures that the rule won’t run on subrequests Subrequests are things like SSI evaluations Image and css requests are NOT subrequests 39
  • 40. Proxy [P] rules are served through a proxy subrequest mod_proxy must be installed for this flag to work RewriteEngine On RewriteRule (.*).(jpg|gif|png) http://images.example.com$1.$2 [P] 40
  • 41. Passthrough [PT] or [passthrough] Hands it back to the URL mapping phase Treat this as though this was the original request 41
  • 42. QSAppend [QSA] or [qsappend] appends to the query string, rather than replacing it. 42
  • 43. Redirect [R] or [redirect] forces a 302 Redirect Note that in this case, the user will see the new URL in their browser This is the default behavior when the target starts with http:// or https:// 43
  • 44. Skip [S=n] or [skip=n] skips the next n RewriteRules This is very weird I’ve never used this in the real world. Could be used as a sort of inverse RewriteCond (viz WordPress) RewriteRule %{REQUEST_FILENAME} -f [S=15] 44
  • 45. Type [T=text/html] Forces the Mime type on the resulting URL Used to do this instead of [H] in some contexts Good to ensure that file-path redirects are handled correctly RewriteRule ^(.+.php)s$ $1 [T=application/x-httpd-php-source] 45
  • 46. RewriteCond Causes a rewrite to be conditional Can check the value of any variable and make the rewrite conditional on that. RewriteCond TestString Pattern [Flags] 46
  • 47. RewriteCond The test string can be just about anything Env vars, headers, or a literal string Backreferences become %1, %2, etc 47
  • 48. Looping Looping occurs when the target of a rewrite rule matches the pattern This results in an infinite loop of rewrites RewriteCond %{REQUEST_URI} !^/example.html RewriteRule ^/example /example.html [PT] 48
  • 49. Conditional rewrites Rewrites conditional on some arbitrary thingy Only first Rule is dependent RewriteEngine on RewriteCond %{TIME_HOUR}%{TIME_MIN} >0700 RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900 RewriteRule ^page.html$ page.day.html RewriteRule ^page.html$ page.night.html 49
  • 50. SSL Rewrites Redirect requests to https:// if the request was for http (In a .htaccess file) RewriteCond %{HTTPS} !ON RewriteRule (.*) https://%{HTTP_HOST}/$1 [R] 50
  • 51. RewriteMap Call an external program, or map file, to perform the rewrite Useful for very complex rewrites, or perhaps ones that rely on something outside of Apache 51
  • 52. RewriteMap - file File of one-to-one relationships RewriteMap docsmap txt:/www/conf/docsmap.txt RewriteRule ^/docs/(.*) ${docsmap:$1} [R,NE] Where docsmap.txt contains: Alias http://httpd.apache.org/docs/mod_alias.html#alias Redirect http://httpd.apache.org/docs/mod_alias.html#redirect ... etc Requests for http://example.com/docs/something now get redirected to the Apache docs site for ‘something’. [NE] makes the #anchor bit work. 52
  • 53. Poor-man’s load balancing Random selection of server for “load balancing” RewriteMap servers rnd:/www/conf/servers.txt RewriteRule (.*) http://${servers:loadbalance}$1 [P,NS] servers.txt contains: loadbalance mars|jupiter|saturn|neptune Requests are now randomly distributed between the four servers. The ‘NS’ ensures that the proxied URL doesn’t get re-rewritten. 53
  • 54. dbm RewriteMap asbury dbm:/usr/local/apache/conf/aliases.map Convert a one-to-one text mapping to a dbm file httxt2dbm utility (2.0) 54
  • 55. RewriteMap - program Call an external program to do the rewrite Perl is a common choice here, due to its skill at handling text. RewriteMap dash2score prg:/usr/local/apache/conf/dash2score.pl RewriteEngine On RewriteRule (.*-.*) ${dash2score:$1} [PT] 55
  • 56. dash2score.pl #!/usr/bin/perl $| = 1; # Turn off buffering while (<STDIN>) { s/-/_/g; # Replace - with _ globally print $_; } Turning off buffering is necessary because we need the output immediately for each line we feed it. Apache starts the script on server startup, and keeps it running for the life of the server process
  • 57. SQL (in 2.3-HEAD) Just committed on Monday Have a SQL statement in the RewriteMap directive which returns the mapping 57
  • 58. .htaccess files .htaccess files are evil However, a lot of people have no choice So ... 58
  • 59. .htaccess files In .htaccess files, or <Directory> scope, everything is assumed to be relative to that current scope So, that scope is removed from the RewriteRule ^/index.html in httpd.conf becomes ^index.html in a .htaccess file or <Directory> scope 59
  • 60. .htaccess files RewriteLog is particularly useful when trying to get .htaccess file RewriteRules working. However, you can’t turn on RewriteLog in a .htaccess file, and presumably you’re using .htaccess files because you don’t have access to the main server config. It’s a good idea to set up a test server on your home PC and test there with RewriteLog enabled 60
  • 61. .htaccess files The rewrite pattern is relative to the current directory The rewrite target is also relative to the current directory In httpd.conf, the rewrite target is assumed to be a file path. In .htaccess files, that file path is relative to the current directory, so it seems to be a URI redirect. 61
  • 62. Further resources http://rewrite.drbacchus.com/ “Definitive Guide to mod_rewrite” by Rich Bowen, from APress http://httpd.apache.org/docs-2.2/rewrite/ 62
  • 64. Bonus slides - Recipes Redirect everything to a central handler 64
  • 65. RewriteEngine On RewriteCond %{REQUEST_URI} !handler.php RewriteRule (.*) /handler.php?$1 [PT,L,NE] All requests are sent to handler.php The request is passed as a QUERY_STRING argument to handler.php so that it knows what was requested. 65
  • 66. Virtual Hosts Rewrite a request to a directory based on the requested hostname. 66
  • 67. RewriteEngine On RewriteCond %{HTTP_HOST} (.*).example.com [NC] RewriteRule (.*) /home/%1/www$1 The hostname ends up in %1 The requested path is in $1 - includes leading slash Will probably have to do special things for handlers (like .php files) 67
  • 68. .phps source handler RewriteRule (.*).phps $1.php [H=application/x-httpd-php-source] Syntax-highlighted code rendering of any .php file 68