Project

General

Profile

Mod redirect » History » Revision 32

Revision 31 (gstrauss, 2020-06-01 06:30) → Revision 32/35 (gstrauss, 2021-07-17 23:32)

h1. URL Redirection 

 {{>toc}} 

 *Module: mod_redirect* 

 h2. Description 

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


 

 h2. Options 

 table{margin-left: 2em}. 
 |_.option |_. description |_. note | 
 | url.redirect | URL path matching rules to generate HTTP Location redirect | | 
 | url.redirect-code | defines the http code that is sent with the redirect URL (default: 301) | (since 1.4.31) | 


 h3. url.redirect-code 

 HTTP status code sent with the HTTP redirect (default: 301) 
 e.g. @url.redirect-code = 302@ 


 h3. url.redirect 

 URL path matching rules to generate HTTP Location redirect Redirects a set of URLs externally 
 <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 = ("" => "https://%1${url.path}${qsa}") 
 } 
 </pre> 
 (more detailed documentation can be found in the [[Docs_ModRewrite#Regular-Expressions|Regular Expression]] section of [[Docs_ModRewrite|mod_rewrite]] doc) 

 Note that the "%1" in the @url.redirect@ target refers to the parenthesized subexpression in the conditional regexp (.*) directly enclosing the @url.redirect@.    It *does not* 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.    A negative match (@!~@) does not save any % patterns. 

 Redirect rules are evaluated up until the first matched redirect rule.    As a special case to allow short-circuiting the redirect rules _without_ triggering a redirect, specify a blank target: <code>url.redirect = ( "^/do-not-redirect/this/path" => "" )</code>.    Then, a further catch-all redirect rule might redirect everything else.    This can be used as an alternative to nested conditions inside other conditions.    The blank target special-case is available in version 1.4.40 or later. 


 h2. Examples 

 [[HowToRedirectHttpToHttps]] - Redirecting any HTTP request to HTTPS aka forcing SSL h3. url.redirect-code 

 (more detailed documentation can be found in Defines the [[Docs_ModRewrite#Regular-Expressions|Regular Expression]] section of [[Docs_ModRewrite|mod_rewrite]] doc) HTTP status code that is sent with the redirect URL. 
 e.g. @url.redirect-code = 302@ (since 1.4.31) 

 h2. Example 

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

 <pre> 
 $HTTP["host"] !~ "^www\." { 
     url.redirect = ("" => "https://www.${url.authority}${url.path}${qsa}") 
 } 
 </pre> 
 Redirect 

 Moving all *.example.org subdomains except a few to www.example.org: 
 

 <pre> 
 $HTTP["host"] !~ "^(?:www|mail|mysql)\.example\.org$" "^(www|mail|mysql)\.example\.org$" { 
     url.redirect = ("" => "https://www.example.org${url.path}${qsa}") 
 }  
 </pre> 
 Redirect example.xxx, www.example.net, www.example.org 

 Redirecting any HTTP request to www.example.com 
 <pre> 
 $HTTP["host"] =~ "^(?:example\.(?:com|net|org)|(?:www\.)?example\.(?:net|org))$" { 
   url.redirect = ( "" => "http://www.example.com${url.path}${qsa}" ) 
 } 
 </pre> 
 In the other direction, redirect www.example.com to example.com 
 <pre> 
 $HTTP["host"] == "www.example.com" { 
   url.redirect    = ( "" => "http://example.com${url.path}${qsa}" ) 
 } 
 </pre> 
 Redirect all domains starting "www." to domains without "www." prefix 
 <pre> 
 $HTTP["host"] =~ "^www\.(.*)$" { 
   url.redirect    = ( "" => "http://%1${url.path}${qsa}" ) 
 } 
 </pre> 
 Similar configurations HTTPS aka forcing SSL: [[HowToRedirectHttpToHttps]] 

 (more detailed documentation can be used to redirect all .net/.org etc. requests to found in the dot-com address for the site: 
 <pre> 
 $HTTP["host"] =~ "^(?:www\.)?example\.(?:net|org)$" { 
   url.redirect    = ( "" => "http://example.com${url.path}${qsa}" ) 
 } 
 </pre> [[Docs_ModRewrite#Regular-Expressions|Regular Expression]] section of [[Docs_ModRewrite|mod_rewrite]] doc)