Skip to content

Commit 4d6d276

Browse files
committed
Add www redirect
1 parent 6d7596e commit 4d6d276

File tree

3 files changed

+150
-2
lines changed

3 files changed

+150
-2
lines changed

features/bootstrap/FeatureContext.php

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
include_once(__DIR__ . '/../../php/class-ee.php');
44
include_once(__DIR__ . '/../../php/utils.php');
55

6+
define( 'EE_CONF_ROOT', '/opt/easyengine' );
7+
68
use Behat\Behat\Context\Context;
79
use Behat\Behat\Hook\Scope\AfterFeatureScope;
810

@@ -65,6 +67,14 @@ public function returnCodeShouldBe0()
6567
}
6668
}
6769

70+
/**
71+
* @Then After delay of :time seconds
72+
*/
73+
public function afterDelayOfSeconds( $time )
74+
{
75+
sleep( $time );
76+
}
77+
6878
/**
6979
* @Then /(STDOUT|STDERR) should return exactly/
7080
*/
@@ -92,7 +102,6 @@ public function stdoutShouldReturnSomethingLike($output_stream, PyStringNode $ex
92102
throw new Exception("Actual output is:\n" . $command_output);
93103
}
94104
}
95-
96105
/**
97106
* @Then The :site db entry should be removed
98107
*/
@@ -179,12 +188,53 @@ public function requestOnShouldContainFollowingHeaders($site, TableNode $table)
179188
}
180189
}
181190

191+
/**
192+
* @Then Request on :host with header :header should contain following headers:
193+
*/
194+
public function requestOnWithHeaderShouldContainFollowingHeaders($host, $header, TableNode $table)
195+
{
196+
197+
$ch = curl_init();
198+
curl_setopt($ch, CURLOPT_URL, $host);
199+
curl_setopt($ch, CURLOPT_HEADER, true);
200+
curl_setopt($ch, CURLOPT_HTTPHEADER, [ $header ]);
201+
curl_setopt($ch, CURLOPT_NOBODY, true);
202+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
203+
$headers = curl_exec($ch);
204+
curl_close($ch);
205+
206+
$rows = $table->getHash();
207+
208+
foreach ($rows as $row) {
209+
if (strpos($headers, $row['header']) === false) {
210+
throw new Exception("Unable to find " . $row['header'] . "\nActual output is : " . $headers);
211+
}
212+
}
213+
}
214+
215+
/**
216+
* @When Site :site has certs
217+
*/
218+
public function siteHasCerts( $site )
219+
{
220+
$certs_dir = EE_CONF_ROOT . '/nginx/certs/';
221+
222+
touch( $certs_dir . $site . '.crt' );
223+
touch( $certs_dir . $site . '.key' );
224+
exec('docker exec ee-nginx-proxy sh -c "/app/docker-entrypoint.sh /usr/local/bin/docker-gen /app/nginx.tmpl /etc/nginx/conf.d/default.conf; /usr/sbin/nginx -s reload"');
225+
}
226+
182227
/**
183228
* @AfterFeature
184229
*/
185230
public static function cleanup(AfterFeatureScope $scope)
186231
{
187-
exec("sudo bin/ee site delete hello.test");
232+
exec("sudo bin/ee site delete hello.test --yes");
233+
exec("sudo bin/ee site delete example.test --yes");
234+
exec("sudo bin/ee site delete www.example1.test --yes");
235+
exec("sudo bin/ee site delete example2.test --yes");
236+
exec("sudo bin/ee site delete www.example3.test --yes");
237+
188238
if(file_exists('ee.phar')) {
189239
unlink('ee.phar');
190240
}

features/redirect.feature

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Feature: Site Redirection
2+
3+
Scenario: no_www-no_ssl redirection works properly
4+
When I run 'bin/ee site create example.test'
5+
Then Request on 'localhost' with header 'Host: www.example.test' should contain following headers:
6+
| header |
7+
| HTTP/1.1 301 Moved Permanently |
8+
| Location: http://example.test/ |
9+
10+
Scenario: www-no_ssl redirection works properly
11+
When I run 'bin/ee site create www.example1.test'
12+
Then Request on 'localhost' with header 'Host: example1.test' should contain following headers:
13+
| header |
14+
| HTTP/1.1 301 Moved Permanently |
15+
| Location: http://www.example1.test/ |
16+
17+
Scenario: no_www-ssl redirection works properly
18+
When I run 'bin/ee site create example2.test --le [email protected] --skip-status-check'
19+
And Site 'example2.test' has certs
20+
Then Request on 'localhost' with header 'Host: www.example2.test' should contain following headers:
21+
| header |
22+
| HTTP/1.1 301 Moved Permanently |
23+
| Location: https://example2.test/ |
24+
And Request on 'localhost:443' with header 'Host: www.example2.test' should contain following headers:
25+
| header |
26+
| HTTP/1.1 301 Moved Permanently |
27+
| Location: https://example2.test/ |
28+
29+
Scenario: www-ssl redirection works properly
30+
When I run 'bin/ee site create www.example3.test'
31+
And Site 'www.example3.test' has certs
32+
Then Request on 'localhost' with header 'Host: www.example3.test' should contain following headers:
33+
| header |
34+
| HTTP/1.1 301 Moved Permanently |
35+
| Location: https://www.example3.test/ |
36+
And Request on 'localhost:443' with header 'Host: example3.test' should contain following headers:
37+
| header |
38+
| HTTP/1.1 301 Moved Permanently |
39+
| Location: https://www.example3.test/ |

src/Site_Command.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,8 @@ private function configure_site() {
704704
$env_content = \EE\Utils\mustache_render( SITE_TEMPLATE_ROOT . '/config/.env.mustache', $env_data );
705705
$php_ini_content = \EE\Utils\mustache_render( SITE_TEMPLATE_ROOT . '/config/php-fpm/php.ini.mustache', [] );
706706

707+
$this->add_site_redirects();
708+
707709
try {
708710
if ( ! ( file_put_contents( $site_docker_yml, $docker_compose_content )
709711
&& file_put_contents( $site_conf_env, $env_content )
@@ -721,6 +723,63 @@ private function configure_site() {
721723
}
722724
}
723725

726+
/**
727+
* Adds www to non-www redirection to site
728+
*/
729+
private function add_site_redirects() {
730+
$confd_path = EE_CONF_ROOT . '/nginx/conf.d/';
731+
$config_file_path = $confd_path . $this->site_name . '-redirect.conf';
732+
$has_www = strpos( $this->site_name, 'www.' ) === 0;
733+
$content = '';
734+
735+
if( $has_www ) {
736+
$site_name_without_www = ltrim( $this->site_name, '.www' );
737+
// ee site create www.example.com --le
738+
if( $this->le ) {
739+
$content = "
740+
server {
741+
listen 80;
742+
listen 443;
743+
server_name $site_name_without_www;
744+
return 301 https://$this->site_name\$request_uri;
745+
}";
746+
}
747+
// ee site create www.example.com
748+
else {
749+
$content = "
750+
server {
751+
listen 80;
752+
server_name $site_name_without_www;
753+
return 301 http://$this->site_name\$request_uri;
754+
}";
755+
}
756+
}
757+
else {
758+
$site_name_with_www = 'www.' . $this->site_name;
759+
// ee site create example.com --le
760+
if( $this->le ) {
761+
762+
$content = "
763+
server {
764+
listen 80;
765+
listen 443;
766+
server_name $site_name_with_www;
767+
return 301 https://$this->site_name\$request_uri;
768+
}";
769+
}
770+
// ee site create example.com
771+
else {
772+
$content = "
773+
server {
774+
listen 80;
775+
server_name $site_name_with_www;
776+
return 301 http://$this->site_name\$request_uri;
777+
}";
778+
}
779+
}
780+
file_put_contents( $config_file_path, ltrim( $content, PHP_EOL ) );
781+
}
782+
724783
/**
725784
* Function to generate default.conf from mustache templates.
726785
*

0 commit comments

Comments
 (0)