Lukáš Bařinka
© 2021
REV 2.10
mod_aliasmod_rewritemod_aliasmod_rewrite3xx):
301 Moved Permanently Use new URL since302 Found Temporary move303 See Other Replaced with new resource305 Use Proxy Resource available through proxy3xx
301 Moved Permanently Trvale přesunuto, odteď použij novou URL302 Found Dočasně přesunuto, příště zase zkus původní URL303 See Other Nahrazeno novým zdrojem305 Use Proxy Použít proxy pro přístup k tomuto zdrojiRedirect directive Redirects client from origin URL to new URLRedirectMatch directive Redirection based on Regular ExpressionRedirectTemp directive = Redirect tempRedirectPermanent directive = Redirect permanentRedirect Přesměrovává klienta ze starého URL na novéRedirectMatch Přesměrovává klienta na základě RERedirectTemp = Redirect tempRedirectPermanent = Redirect permanentRedirect directives takes precedence before Alias and ScriptAlias directives.
Direktivy Redirect mají přednost před Alias, ScriptAlias.
Redirect [status] URLpath newURL
302permanent = 301temp = 302seeother = 303gone = 410 Resource does not exist any more, no newURL parameter302permanent = 301temp = 302seeother = 303gone = 410 Zdroj již neexistuje, nepoužívá se část newURLwww.mycorp.net/other to another server www.othercorp1.net
Redirect permanent /other www.othercorp1.net
.htm extension to .html extension For www.mycorp.net server
RedirectMatch (.*)\.htm$ www.mycorp.net$1.html
/ (root) to /about/ directory
RedirectMatch ^/$ http://www.mycorp.net/about/
www.mycorp.net/other na server www.othercorp1.net
Redirect permanent /other www.othercorp1.net/
.htm na požadavky končící na .html Pro server www.mycorp.net
RedirectMatch (.*)\.htm$ www.mycorp.net$1.html
/ (kořen) na adresář /about/
RedirectMatch ^/$ http://www.mycorp.net/about/
mod_rewrite modulemod_rewriteSimple redirection cases can be configured using Redirect directive (mod_alias)
Jednoduché případy lze nahradit pomocí Redirect (mod_alias)
RewriteEngine on|off
Context: server, virtualhost, directory, .htaccess
Options FollowSymLinks must be enabled to use per-directory rewriting (for security reasons)
RewriteRule Pattern Substitution [flags]
RewriteRule ^/somepath(.*) /otherpath$1 [R]
RewriteCond TestString CondPattern
RewriteCond %{TIME_HOUR}%{TIME_MIN} <0700
RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900
RewriteEngine on|off
Kontext: server, virtualhost, directory, .htaccess
Pro povolení přepisování uvnitř adresářů je nezbytné nastavení Options FollowSymLinks (z důvodů bezpečnosti)
RewriteRule Pattern Substitution [flags]
RewriteRule ^/somepath(.*) /otherpath$1 [R]
RewriteCond TestString CondPattern
RewriteCond %{TIME_HOUR}%{TIME_MIN} <0700
RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900
.htaccess example.htaccessExample of .htaccess file in CMS Drupal 8 in …/sites/default/files
Ukázka .htaccess souboru v CMS Drupal 8 ve …/sites/default/files
RewriteEngine On
# Address to be rewritten inside directory contains filename only;
# therefore conditions are based on variables in this case
# If request targets to CMS static file …
RewriteCond expr "%{REQUEST_URI} -strmatch '/sites/default/files/*'"
# … and requested file does not exist at the same time
RewriteCond %{REQUEST_FILENAME} !-f
# Redirect request to another server
RewriteRule (.*) https://oldweb/sites/default/files/$1 [R]
RewriteEngine On
# Adresa pro přepis obsahuje pouze jméno souboru v adresáři
# proto je potřeba v podmínkách použít proměnné
# Pokud adresa směřuje do CMS na statický soubor
RewriteCond expr "%{REQUEST_URI} -strmatch '/sites/default/files/*'"
# A zároveň požadovaný soubor neexistuje
RewriteCond %{REQUEST_FILENAME} !-f
# Přesměruj požadavek na starý server
RewriteRule (.*) https://oldweb/sites/default/files/$1 [R]
Example of configuration that upgrades HTTP connection to HTTPS connection for addresses starting with /secure/ location
Ukázka konfigurace, která provede povýšení spojení z HTTP na HTTPS, pokud adresa začíná na /secure/
# This will enable the Rewrite capabilities
RewriteEngine On
# This checks to make sure the connection is not already HTTPS
RewriteCond %{HTTPS} !=on
# This rule will redirect all users who are using any part of
# /secure/ to the same location but using HTTPS.
RewriteRule ^/?secure/(.*) https://%{SERVER_NAME}/secure/$1 [R,L]
RewriteEngine On
# Podmínka: pokud spojení není přes HTTPS
RewriteCond %{HTTPS} !=on
# Pak všechny adresy se začátkem /secure/ nebo secure/
# budou přesměrovány na stejnou adresu, ale s HTTPS
RewriteRule ^/?secure/(.*) https://%{SERVER_NAME}/secure/$1 [R,L]
RewriteOptions Options
Context: server, virtualhost, directory, .htaccess
Inherit Disabled by default, more predictable behavior
RewriteOptions Inherit
Inherited rules are applied after current context rules
InheritBefore Likewise Inherit, but inherited rules are applied in advanceIgnoreInherit Ignoring inherited rules
RewriteOptions Options
Kontext: server, virtualhost, directory, .htaccess
Inherit Standardně vypnuté, více předvídatelné chování
RewriteOptions Inherit
Zděděná pravidla se aplikují až po pravidlech daného kontextu
InheritBefore Jako Inherit, ale zděděná pravidla se aplikují před pravidly daného kontextuIgnoreInherit Ignoruje zděděná pravidla
LogLevel level
Range: ≤debug: nothing, >trace2: for debugging purposes only
LogLevel alert rewrite:trace3
tail -f errorlog | fgrep '[rewrite:'
LogLevel level
Rozsah: ≤debug: nic, >trace2: pouze pro účely ladění
LogLevel alert rewrite:trace3
tail -f errorlog | fgrep '[rewrite:'
RewriteRule directive Rewrites original URL to new URL according to pattern
RewriteRule Pattern Substitution [flags]
/var/www/new | pathname (File-path) |
/img | Address (URL-path) |
http://www.mycorp.net/new | Absolute URL |
– | Nothing (dash) |
$N | Nth RewriteRule subexpression |
%N | Nth RewriteCond subexpression |
%{VARNAME} | Server variable |
${mapname:key|default} | Mapping function (with default value) |
RewriteRule Přepíše původní adresu na novou adresu na základě vzoru
RewriteRule Pattern Substitution [flags]
/var/www/new | Cesta (file-path) |
/img | Adresa (URL-path) |
http://www.mycorp.net/new | Absolutní URL |
– | Nic (pomlčka) |
$N | N-tý podvýraz v RewriteRule |
%N | N-tý podvýraz v RewriteCond |
%{VARNAME} | Proměnná serveru |
${mapname:key|default} | Mapovací funkce (s výchozí hodnotou) |
chain|C
Chain with next rule, if fails → skip chain
Zřetězí s dalším pravidlem,
env|E=VAR:VAL
Set environment variable
Nastavuje proměnnou prostředí
forbidden|F
Return 403 Forbidden response
Vrací odpověď 403 Forbidden
last|L
Last rule (skip the rest)
Poslední pravidlo (další nezpracovává)
next|N
Start over again from the top (!)
Začne znova přepisovat od začátku (!)
nocase|NC
Process in case-insensitive manner
Zpracovává bez ohledu na velikost
noescape|NE
Prevent URL encoding (%xx)
Nepřevádí speciální znaky na %xx
proxy|P
Force to use proxy (implies [L])
Vynucuje použití proxy (implikuje [L])
passthrough|PT
Pass back through URL mapping
Pošle zpět na WS→FS mapování
qsappend|QSA
Append original query string to URL
Připojí původní query string za URL
redirect|R [=code]
Force (external) redirection
Vynucuje externí přesměrování
skip|S=num
Skip next num rules
Přeskočí dalších num pravidel
type|T=MIME-type
Set response MIME type
Nastavuje MIME typ odpovědi
Forbid all characters in URL expect listed ones E.g. lowercase characters and / . ö ü
Zákaz všech znaků v adrese kromě vybraných Např.: malých písmen a / . ö ü
RewriteEngine On
RewriteRule "^([a-z/.]|\xc3\xb6|\xc3\xbc)+$" "-" [S=1]
RewriteRule .* - [F]
ö | u00f6 | \xc3\xb6 |
ü | u00fc | \xc3\xbc |
ë | u00eb | \xc3\xab |
<Directory> or .htaccess processes rules for resources inside directory onlyRewriteBase directive
RewriteBase URL-path
Context: directory, .htaccess
<Directory> nebo .htaccess se vzor pravidla týká pouze daného adresářeRewriteBase
RewriteBase URL-path
Kontext: directory, .htaccess
RewriteMap directive
Defines map function for rewriting
txttext filerndtext file with random selectiondbmbinary fileintinternal functionprgexternal programRewriteRule (substitution)
${ MapName : LookupKey }
${ MapName : LookupKey | DefaultValue }
RewriteMap examplemap txt:/path/to/file/map.txt
RewriteRule ^/ex/(.*) ${examplemap:$1}
RewriteMap Definuje mapu pro přepis
txttextový souborrndtextový soubor s náhodným výběremdbmbinární souborintinterní funkceprgexterní programRewriteRule (substituci)
${ MapName : LookupKey }
${ MapName : LookupKey | DefaultValue }
RewriteMap examplemap txt:/path/to/file/map.txt
RewriteRule ^/ex/(.*) ${examplemap:$1}