You are viewing a plain text version of this content. The canonical link for it is here.
Posted to wiki-changes@httpd.apache.org by Apache Wiki <wi...@apache.org> on 2007/05/31 11:41:28 UTC

[Httpd Wiki] Update of "Rewrite/WhenNotToUseRewrite" by VinkoVrsalovic

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Httpd Wiki" for change notification.

The following page has been changed by VinkoVrsalovic:
http://wiki.apache.org/httpd/Rewrite/WhenNotToUseRewrite

The comment on the change is:
added aliasmatch, trivial regexp adjustment, and assumptions for the examples

------------------------------------------------------------------------------
  Given the power and flexibility of mod_rewrite it's not surprising that people often use it as their [http://en.wikipedia.org/wiki/Golden_hammer golden hammer]. Part of gaining knowledge about mod_rewrite is knowing when not to use it.
  
- The following is a list of misuses of mod_rewrite and more appropriate (and often faster) alternatives.
+ The following is a list of misuses of mod_rewrite and more appropriate (and often faster) alternatives. These examples assume the {{{RewriteRules}}} are not placed in a directory context (ie, not in {{{.htaccess}}} or {{{<Directory>}}}).
  
  === Simple Redirects ===
  
@@ -17, +17 @@

  
  {{{
  # redirect and drop the trailing uri
- RewriteRule ^/(.*) http://other.example.com/
+ RewriteRule ^/.* http://other.example.com/
  
- # is better expressed as..
+ # is better expressed as.. (we only need to match every possible string 
+ # without storing it, thus just ^)
- RedirectMatch (.*) http://other.example.com/
+ RedirectMatch ^ http://other.example.com/
  }}}
  
  {{{
@@ -31, +32 @@

  RedirectMatch ^/foo/(.+)/bar http://other.example.com/$1
  }}}
  
- Valid uses for mod_rewrite when redirect include:
+ Valid uses of mod_rewrite for redirect include:
   * Redirecting based on the query string
   * Redirecting based on other request details such as user-agents.
  
@@ -44, +45 @@

  Alias /foo /var/www/bar
  }}}
  
- TODO, add AliasMatch examples
+ or
+ 
+ {{{
+ RewriteRule ^/foo/(.*)/baz /bar/$1
+ 
+ # is better expressed as..
+ AliasMatch /foo/(.*)/baz /var/www/bar/$1
+ }}}
+ 
+ Valid uses of mod_rewrite for aliasing include:
+  * Aliasing based on complex rules 
+ 
+ TODO, validate !AliasMatch example, add more valid uses or expand them :)
  
  === Proxying ===