<IfModule mod_ssl.c>

## Homeassistant
<Virtualhost *:443>
  ServerName gugg.siora.de

  <IfModule mod_rewrite.c>
    ProxyPreserveHost On
    ProxyRequests Off
    RewriteEngine On

    ## RPi -> FB NAS
    RewriteRule /nas(.*)$  http://10.1.1.254/nas/$1 [P]
    ProxyPassReverse /nas/  http://10.1.1.254/nas [P]

    ## RPi Cam Control
    RewriteRule ^(cam)($|/) - [N]
    RewriteCond %{REQUEST_URI} ^/cam
    RewriteCond %{HTTP:Upgrade} =websocket [NC]
    RewriteRule /cam/(.*)$  ws://rpi-cam/cam/$1 [P,L]
    RewriteCond %{HTTP:Upgrade} !=websocket [NC]
    RewriteRule /cam/(.*)$  http://rpi-cam/cam/$1 [P,L]

    ## Home-assistant: rewrite to http://<docker-container>
    RewriteCond %{HTTP:Upgrade} =websocket [NC]
    # exclude /cam from rewriting:
    #RewriteCond %{REQUEST_URI} !^/cam (not needed since above rewrite rules terminate with [L])
    RewriteRule /(.*)  ws://172.17.0.1:8123/$1 [P,L]
    RewriteCond %{HTTP:Upgrade} !=websocket [NC]
    # exclude /cam from rewriting:
    #RewriteCond %{REQUEST_URI} !^/cam (not needed since above rewrite rules terminate with [L])               
	RewriteRule /(.*)  http://172.17.0.1:8123/$1 [P,L]

  </IfModule>


  SSLCertificateFile /etc/letsencrypt/live/gugg.siora.de/fullchain.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/gugg.siora.de/privkey.pem
  Include /etc/letsencrypt/options-ssl-apache.conf
</Virtualhost>

## Trilium
<Virtualhost *:443>
  ServerName notes.siora.de

  <IfModule mod_rewrite.c>
    ProxyPreserveHost On
    ProxyRequests Off
    RewriteEngine On

    RewriteRule /(.*)  http://127.0.0.1:8081/$1 [P,L]
    RewriteCond %{HTTP:Connection} Upgrade [NC]
    RewriteCond %{HTTP:Upgrade} websocket [NC]
    RewriteRule /(.*) ws://localhost:8081/$1 [P,L]
    AllowEncodedSlashes NoDecode
    ProxyPass / http://localhost:8081/ nocanon
 	ProxyPassReverse / http://127.0.0.1:8081/notes
  </IfModule>

  SSLCertificateFile /etc/letsencrypt/live/notes.siora.de/fullchain.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/notes.siora.de/privkey.pem
  Include /etc/letsencrypt/options-ssl-apache.conf
</Virtualhost>

</IfModule>
