Skip to content

Commit

Permalink
Add reverse_www filter to fix www_redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
fullyint committed Feb 18, 2016
1 parent ea3adaa commit 18de57e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Expand Up @@ -9,3 +9,6 @@ end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.py]
indent_size = 4
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
.vault_pass
.vagrant
vendor/roles
*.py[co]
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -1,4 +1,5 @@
### HEAD
* Add `reverse_www` filter to fix `www_redirect` ([#486](https://github.com/roots/trellis/pull/486))
* Add IP address variable, move some variables to top of Vagrantfile ([#494](https://github.com/roots/trellis/pull/494))
* Keep Composer updated ([#493](https://github.com/roots/trellis/pull/493))
* Use prestissimo Composer plugin ([#492](https://github.com/roots/trellis/pull/492))
Expand Down
38 changes: 38 additions & 0 deletions filter_plugins/trellis_filters.py
@@ -0,0 +1,38 @@
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import types

from ansible import errors
from ansible.compat.six import string_types

def reverse_www(value):
''' Add or remove www subdomain '''

# Check if value is a list and parse each item
if isinstance(value, (list, tuple, types.GeneratorType)):
values = []
for item in value:
values.append(reverse_www(item))
return values

# Add or remove www
elif isinstance(value, string_types):
if value.startswith('www.'):
return value[4:]
else:
return 'www.{0}'.format(value)

# Handle invalid input type
else:
raise errors.AnsibleFilterError('The reverse_www filter expects a string or list of strings, got ' + repr(value))


class FilterModule(object):
''' Trellis jinja2 filters '''

def filters(self):
return {
'reverse_www': reverse_www,
}
4 changes: 2 additions & 2 deletions roles/wordpress-setup/templates/wordpress-site.conf.j2
Expand Up @@ -95,7 +95,7 @@ server {
{% if item.value.ssl is defined and item.value.ssl.enabled | default(False) %}
server {
listen 80;
server_name {{ item.value.site_hosts | join(' ') }};
server_name {{ item.value.site_hosts | join(' ') }} {{ item.value.site_hosts | reverse_www | join(' ') }};
return 301 https://$host$request_uri;
}
{% endif %}
Expand All @@ -108,7 +108,7 @@ server {
listen 80;
{% endif -%}

server_name {{ host | match('^www\\.(.*)') | ternary(host | regex_replace('^www\\.(.*)', '\\1'), 'www.' + host ) }};
server_name {{ host | reverse_www }};
return 301 $scheme://{{ host }}$request_uri;
}
{% endfor %}

0 comments on commit 18de57e

Please sign in to comment.