Project

General

Profile

Actions

Mod redirect » History » Revision 34

« Previous | Revision 34/36 (diff) | Next »
gstrauss, 2021-08-26 15:53


URL Redirection

Module: mod_redirect

Description

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

Options

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)

url.redirect-code

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

If the client should use the same method (e.g. POST) to send a request to the redirect target, then code 307 or 308 should be used.
e.g. url.redirect-code = 307
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status

url.redirect

URL path matching rules to generate HTTP Location redirect

url.redirect = (
  "^/show/([0-9]+)/([0-9]+)$" => "https://www.example.org/show.php?isdn=$1&page$2",
  "^/get/([0-9]+)/([0-9]+)$"  => "https://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}")
}

(more detailed documentation can be found in the Regular Expression section of 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: 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.

Examples

HowToRedirectHttpToHttps - Redirecting any HTTP request to HTTPS aka forcing SSL

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

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

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

Redirect all *.example.org subdomains except a few to www.example.org:
$HTTP["host"] !~ "^(?:www|mail|mysql)\.example\.org$" {
    url.redirect = ("" => "https://www.example.org${url.path}${qsa}")
} 

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

In the other direction, redirect www.example.com to example.com
$HTTP["host"] == "www.example.com" {
  url.redirect  = ( "" => "https://example.com${url.path}${qsa}" )
}

Redirect all domains starting "www." to domains without "www." prefix
$HTTP["host"] =~ "^www\.(.*)$" {
  url.redirect  = ( "" => "https://%1${url.path}${qsa}" )
}

Similar configurations can be used to redirect all .net/.org etc. requests to the dot-com address for the site:
$HTTP["host"] =~ "^(?:www\.)?example\.(?:net|org)$" {
  url.redirect  = ( "" => "https://example.com${url.path}${qsa}" )
}

Updated by gstrauss about 3 years ago · 34 revisions