URL Redirection

Module: mod_redirect


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



Redirects a set of URLs externally

(more detailed documentation can be found in the Regular Expression section of mod_rewrite doc)


url.redirect = ( "^/show/([0-9]+)/([0-9]+)$" => "$1&page$2",
                 "^/get/([0-9]+)/([0-9]+)$"  => "$1&page$2" )

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

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.

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: url.redirect = ( "^/do-not-redirect/this/path" => "" ). 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.

url.redirect-code (Added in 1.4.31)

Defines the http code that is sent with the redirect URL


url.redirect-code = 302


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

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

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

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

Redirecting any HTTP request to HTTPS aka forcing SSL: HowToRedirectHttpToHttps

Updated by gstrauss almost 2 years ago · 28 revisions