diff --git a/bin/mock b/bin/mock index 522e3d9e..79fd89d7 100755 --- a/bin/mock +++ b/bin/mock @@ -47,6 +47,7 @@ helpers.testDirectory(outputDir, function(err) { ssl: true, name: 'Example.com', domain: 'example.com', + www: false, roles: WordpressGenerator.roles.map(function(role) { return role.value; }), ip: '192.168.137.137', DB_NAME: 'example_db', diff --git a/lib/yeoman/index.js b/lib/yeoman/index.js index a76b1453..1c971cf6 100644 --- a/lib/yeoman/index.js +++ b/lib/yeoman/index.js @@ -223,6 +223,39 @@ var WordpressGenerator = yeoman.generators.Base.extend({ }); }, + promptForWww: function() { + var choices = [ + { + name: 'No, just the domain', + value: false, + }, + { + name: 'Yes, www is preferred', + value: true, + }, + ]; + var existing = function() { + try { + var match = this.readFileAsString(path.join(this.env.cwd, 'lib', 'ansible', 'group_vars', 'all')) + .match(/www[ ]*:[ ]*(true)/i); + + return match[1]; + } catch(e) {}; + }.bind(this); + + if(existing()) { + choices.reverse(); + } + + this.prompts.push({ + required: true, + type: 'list', + name: 'www', + message: 'Should this site enforce a www subdomain in production? (e.g. www.mysite.com)', + choices: choices + }); + }, + promptForRoles: function() { try { var provisionYml = this.readFileAsString(path.join(this.env.cwd, 'lib', 'ansible', 'provision.yml')); diff --git a/lib/yeoman/templates/lib/ansible/group_vars/all b/lib/yeoman/templates/lib/ansible/group_vars/all index f5c85ba9..d05eb36d 100644 --- a/lib/yeoman/templates/lib/ansible/group_vars/all +++ b/lib/yeoman/templates/lib/ansible/group_vars/all @@ -1,6 +1,7 @@ --- evolve_version: <%= pkg.version %> domain: <%= props.domain %> +www: <%= props.www %> mysql: name: <%= props.DB_NAME %> diff --git a/lib/yeoman/templates/lib/capistrano/deploy.rb b/lib/yeoman/templates/lib/capistrano/deploy.rb index 25d21d57..2348c626 100644 --- a/lib/yeoman/templates/lib/capistrano/deploy.rb +++ b/lib/yeoman/templates/lib/capistrano/deploy.rb @@ -15,6 +15,7 @@ set :deploy_to, "/var/www/#{fetch(:domain)}/#{fetch(:stage)}/#{fetch(:branch)}" set :linked_dirs, %w{web/wp-content/uploads} set :wp_path, "#{release_path}/web/wp" +set :www, <%= props.www ? 'true' : 'false' %> namespace :deploy do after :updated, :bower_install do diff --git a/lib/yeoman/templates/web/_htaccess b/lib/yeoman/templates/web/_htaccess index f9717f37..8b5548c4 100644 --- a/lib/yeoman/templates/web/_htaccess +++ b/lib/yeoman/templates/web/_htaccess @@ -2,6 +2,10 @@ RewriteEngine On RewriteBase / +# Force <%= props.www ? '' : 'non-' %>www for production +RewriteCond %{HTTP_HOST} ^<%= props.www ? '' : 'www\\.' %><%= props.domain.replace('.', '\\.') %>$ [NC] +RewriteCond %{HTTP:X-Forwarded-Proto} ^http(s)| [NC] +RewriteRule ^(.*)$ http%1://<%= props.www ? 'www.' : '' %><%= props.domain %>/$1 [L,R=301,NC] # Prevent spidering of non-canonical URLs, such as `[*.]staging.*` and `production.*`. RewriteCond %{HTTP_HOST} (^local|staging|^production)\.<%= props.domain.replace('.', '\\.') %>$ [NC] RewriteRule ^robots.txt$ no_robots.txt [L]