Redirection & Apache Virtual Host files
Redirect worden in Apache verzorgd middels de mod_alias- en mod_rewrite-modules. Eenvoudige redirect zoals <oude pagina> → <nieuwe pagina>, gaan met de mod_alias-module.
302 Redirect Directive
De Redirect directive is onderdeel van mod_alias. Voorbeeld [1]:
<VirtualHost *:80> ServerName www.domain1.com Redirect / http://www.domain2.com </VirtualHost>
- Hierbij wordt de root van domain1.com doorgestuurd naar domain2.com
- Let op: Dit is een 302 redirect oftewel een temporary redirect
301 Redirect Directive
Als hierboven, maar nu als permanent redirect:
<VirtualHost *:80> ServerName www.domain1.com Redirect 301 / http://www.domain2.com </VirtualHost>
of
<VirtualHost *:80> ServerName www.domain1.com Redirect permanent / http://www.domain2.com </VirtualHost>
RedirectMatch Directive
Met de RedirectMatch Directive kun je 301- & 302-redirects vormen met behulp van reguliere expressies en substituties. Het is een stuk flexibeler dan gewone redirects, maar nog niet zo flexibel als de mod_rewrite Redirects uit het volgende hoofdstuk.
Voorbeeld 302-redirect:
<VirtualHost *:80> ServerName example.com ServerAlias www.example.com RedirectMatch ^/images/(.*)$ http://images.example.com/$1
- Wat hier gebeurd: Alle URL's die beginnen met "/images/", worden doorgestuurd naar "http://images.example.com/". De tekst achter "/images/" wordt daarbij gekopiëerd.
- Voorbeeld:
example.com/images/blub.jpg
→images.example.com/blub.jpg
.
Dit is hoe je hier een 301-redirect van maakt (niet getest):
<VirtualHost *:80> ServerName example.com ServerAlias www.example.com RedirectMatch 301 ^/images/(.*)$ http://images.example.com/$1
of
<VirtualHost *:80> ServerName example.com ServerAlias www.example.com RedirectMatch permanent ^/images/(.*)$ http://images.example.com/$1
Je hebt een '/' nodig
Dit werkt niet (geeft een foutmelding bij herstarten Apache):
RedirectMatch ^(.*)blub(.*)$ /shop/
Dit werkt wel (en de regex werkt ook):
RedirectMatch ^(.*)blub(.*)$ /shop/
Precedence & order
Regels worden uitgevoerd in de volgorde waarin ze in de hostfile staan - Dus zet de meest specifieke regels bovenaan.
Voorbeeld:
RedirectMatch ^(.*)blub2(.*)$ /shop/2/ RedirectMatch ^(.*)blub(.*)$ /shop/1/
- Als je naar ...blub2... gaat, beland je op /shop/2/
- Als je naar ...blub... gaat, beland je ook op /shop/2/
Als je de volgorde van de regels omdraait:
RedirectMatch ^(.*)blub(.*)$ /shop/1/ RedirectMatch ^(.*)blub2(.*)$ /shop/2/
- Als je naar ...blub2... gaat, beland je op /shop/2/
- Als je naar ...blub... gaat, beland je op /shop/1/
mod_rewrite Redirects - Niet vanuit virtual host files?
mod_rewrite-redirects zijn de meest flexibele redirects. Ze zijn ook de meest complexe:
- Per redirect moeten de voorwaarden gedefineerd worden, waaronder de betreffende redirect actief is
- Redirects kennen een precedence, waarmee je kunt aangeven welke regels belangrijker zijn. Zo kun je bv. zorgen dat meer specifieke regels belangrijker zijn dan meer algemene regels.
Verder: A rewrite rule can be invoked in httpd.conf or in .htaccess [2] - Niet in virtual host bestanden?
Wanneer gebruiken?
- "mod_rewrite should be considered a last resort, when other alternatives are found wanting. Using it when there are simpler alternatives leads to configurations which are confusing, fragile, and hard to maintain. Understanding what other alternatives are available is a very important step towards mod_rewrite mastery." [3]
- Als je geen toegang hebt tot httpd.conf of virtual host-files [4]
Bronnen
- https://www.google.com/search?q=page+redirect+apache+virtual+host+file
- https://www.digitalocean.com/community/tutorials/how-to-create-temporary-and-permanent-redirects-with-apache-and-nginx
- https://www.digitalocean.com/community/tutorials/how-to-set-up-mod_rewrite - Only uses .htaccess
- https://serverfault.com/questions/569737/getting-started-with-apache-mod-rewrite-on-a-virtual-host
- https://serverfault.com/questions/214512/redirect-change-urls-or-redirect-http-to-https-in-apache-everything-you-ever
- https://htaccess.madewithlove.be/ - .htaccess-tester
- https://www.askapache.com/htaccess/crazy-advanced-mod_rewrite-tutorial/
- https://httpd.apache.org/docs/2.4/rewrite/remapping.html
- https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html