Project

General

Profile

Mod redirect » History » Revision 24

Revision 23 (icy, 2009-09-22 19:06) → Revision 24/34 (icy, 2012-08-11 10:42)

h1. URL Redirection 

 {{>toc}} 

 *Module: mod_redirect* 

 h2. Description 

 The redirect module is used to specify redirects for a set of URLs. 

 h2. Options 

 h3. url.redirect 

 Redirects a set of URLs externally 
  
 e.g. 

 <pre> 
 url.redirect = ( "^/show/([0-9]+)/([0-9]+)$" => "http://www.example.org/show.php?isdn=$1&page$2", 
                  "^/get/([0-9]+)/([0-9]+)$"    => "http://www.example.org/get.php?isdn=$1&page$2" ) 

 # make an external redirect 
 # from any www.host (with www.) to the host (without www.) 
 $HTTP["host"] =~ "^www\.(.*)$" { 
   url.redirect = ( "^/(.*)" => "http://%1/$1" ) 
 } 
 </pre> 

 Note that the "%1" in the url.redirect target refers to the parenthesized subexpression in the conditional regexp (.*).    It does *not* necessarily have the meaning that "%1" would have in evhost.path-pattern (where it would mean 'top-level domain').    If url.redirect is specified within a regex conditional, % patterns are replaced by the corresponding groups from the condition regex.    %1 is replaced with the first subexpression, %2 with the second, etc.    %0 is replaced by the entire substring matching the regexp.    See above and below for examples using % patterns. 

 h3. url.redirect-code (Added in 1.5.0) 

 Defines the http code that is sent with the redirect URL 

 e.g. 

 <pre> 
 url.redirect-code = 302 
 </pre> 

 h2. Example 

 Some people love the www part in the url. A general solution to move all non www. hosts to its www equivalent: :: 

 <pre> 
 $HTTP["host"] =~ "^([^.]+\.[^.]+)$" { 
   url.redirect = ( 
     ".*" => "http://www.%1" 
   ) 
 } 
 </pre> 

 Moving any subdomains except a few to www.example.org: (Note: The second match is required since a non-match doesn't set any groups) :: 

 <pre> 
 $HTTP["host"] !~ "^(www|mail|mysql)\.(example\.org)$" { 
   $HTTP["host"] =~ "^(.+\.)?(example\.org)$" { 
     url.redirect = ( 
       "^/(.*)" => "http://www.%2/$1" 
     ) 
   } 
 }  
 </pre> 

 Redirecting any HTTP request to HTTPS aka forcing SSL: [[HowToRedirectHttpToHttps]]