From 63c6e9e83e72b8a7e01677f53a1200fc2f021e28 Mon Sep 17 00:00:00 2001 From: Robert de Bock Date: Thu, 19 Dec 2024 09:11:19 +0100 Subject: [PATCH] Allow redirects.s --- README.md | 6 ++++++ molecule/default/converge.yml | 6 ++++++ templates/vhost.conf.j2 | 14 +++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2063301..d93f8e6 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,12 @@ This example is taken from [`molecule/default/converge.yml`](https://github.com/ - name: my_location location: /my_location backend_url: "http://localhost:8080/myapplication" + httpd_vhosts: + - name: redirect_to_https + servername: www.example.com + port: 8080 + redirect_to_https: true + ssl_port: 8443 # httpd_vhosts: # - name: my_vhost_docroot # servername: www1.example.com diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index 5fa106e..88898de 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -17,6 +17,12 @@ - name: my_location location: /my_location backend_url: "http://localhost:8080/myapplication" + httpd_vhosts: + - name: redirect_to_https + servername: www.example.com + port: 8080 + redirect_to_https: true + ssl_port: 8443 # httpd_vhosts: # - name: my_vhost_docroot # servername: www1.example.com diff --git a/templates/vhost.conf.j2 b/templates/vhost.conf.j2 index 342a6cd..164863c 100644 --- a/templates/vhost.conf.j2 +++ b/templates/vhost.conf.j2 @@ -10,13 +10,24 @@ {% endif %} - +{% if item.redirect_to_https is defined and item.redirect_to_https %} + + LoadModule rewrite_module {{ httpd_modules_path }}/mod_rewrite.so + +{% endif %} + + ServerName {{ item.servername }} {% if item.serveralias is defined %} ServerAlias {{ item.serveralias | join(' ') }} {% endif %} +{% if item.redirect_to_https is defined and item.redirect_to_https %} + RewriteEngine On + RewriteCond %{HTTPS} off + RewriteRule ^(.*)$ https://%{HTTP_HOST}:{{ item.ssl_port | default(httpd_ssl_port) }}%{REQUEST_URI} [L,R=301] +{% else %} {% if item.options is defined %} Options {{ item.options|join(' ') }} {% endif %} @@ -48,4 +59,5 @@ ProxyPass / {{ item.backend_url }} SetEnv {{ env.name }} {{ env.value | default("") }} {% endfor %} {% endif %} +{% endif %}