Skip to content

Commit

Permalink
add option to redirect to root-page on 404
Browse files Browse the repository at this point in the history
  • Loading branch information
ansibleguy committed Sep 11, 2024
1 parent 78981aa commit a48e0db
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 2 additions & 0 deletions defaults/main/2_site.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ defaults_site:
server:
path: '/var/www/html'
index: ['index.html']
e404_try_index: false # if wrong location is entered; serve content of index-file instead of error-page
e404_redirect_root: false # overwrite the 404 error-page with a redirect to the root page

ssl:
mode: 'snakeoil' # existing/selfsigned/ca/letsencrypt/snakeoil
Expand Down
8 changes: 7 additions & 1 deletion templates/etc/nginx/sites-available/inc/site_https.j2
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ server {

{% elif site.mode in ['serve', 'server'] %}
# serve-mode config
try_files $uri $uri.html $uri/ /{{ site.serve.index[0] | default(site.server.index[0]) }} =404;
try_files $uri $uri.html $uri/ {% if site.server.e404_try_index | bool %}/{{ site.serve.index[0] | default(site.server.index[0]) }} {% endif %}=404;
{% endif %}

{% if site.config_additions_root | length > 0 %}
Expand All @@ -93,6 +93,12 @@ server {

}

{% if site.mode in ['serve', 'server'] and site.server.e404_redirect_root | bool %}
# redirect to root-page unknown site was opened
error_page 404 = @redirect_404;
location @redirect_404 { return 302 /; }

{% endif %}
{% if site.config_additions | length > 0 %}
# additional lines
{% endif %}
Expand Down

0 comments on commit a48e0db

Please sign in to comment.