Skip to content

Commit

Permalink
fix: create a multisite htaccess file when enabled
Browse files Browse the repository at this point in the history
htaccess files require different configuration depending on whether
multisite is enabled and based on which type - subfolder or subdomain.
Previously only a single site htaccess file was created no matter the
config. To correct this I've introduced two multisite .htaccess
templates that will be used when their type is selected.

* Added subdomain .htaccess
* Added subfolder htaccess
* Change dockerfile tempalte to use htaccess name variable
* Change config creation to pass htaccess file name to template
  • Loading branch information
ampersarnie committed Aug 7, 2021
1 parent 0227fd2 commit bea9559
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
2 changes: 2 additions & 0 deletions config/.htaccess
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# WP-Cypresss Single Site
# This file is a template for the WP-Cypress package. It will be renamed to `.htaccess`.
# BEGIN WordPress
# The directives (lines) between `BEGIN WordPress` and `END WordPress` are
# dynamically generated, and should only be modified via WordPress filters.
Expand Down
21 changes: 21 additions & 0 deletions config/.htaccess-subdomain
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# WP-Cypresss Multisite Subdomain
# This file is a template for the WP-Cypress package. It will be renamed to `.htaccess`.
# BEGIN WordPress
# The directives (lines) between `BEGIN WordPress` and `END WordPress` are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
# END WordPress
20 changes: 20 additions & 0 deletions config/.htaccess-subfolder
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# WP-Cypresss Multisite Subfolder
# This file is a template for the WP-Cypress package. It will be renamed to `.htaccess`.
# BEGIN WordPress
# The directives (lines) between `BEGIN WordPress` and `END WordPress` are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
# END WordPress
9 changes: 9 additions & 0 deletions lib/modules/createConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ const createConfig = async (packageDir, hasVolumeSupport = true) => {
},
);

let htaccessFile = '.htaccess';

if (config.multisite === 'subdomain') {
htaccessFile = `${htaccessFile}-subdomain`;
} else if (config.multisite) {
htaccessFile = `${htaccessFile}-subfolder`;
}

await renderTemplate(
`${packageDir}/lib/templates/dockerfile.ejs`,
`${packageDir}/Dockerfile`,
Expand All @@ -139,6 +147,7 @@ const createConfig = async (packageDir, hasVolumeSupport = true) => {
versions: validVersions,
vip: config.muPlugins ? config.muPlugins.vip : false,
phpVersion: config.phpVersion || 7.3,
htaccessFile,
},
);

Expand Down
2 changes: 1 addition & 1 deletion lib/templates/dockerfile.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ COPY update.sh /var/www/html

COPY config/php.ini /usr/local/etc/php

COPY config/.htaccess /var/www/html
COPY config/<%= htaccessFile %> /var/www/html/.htaccess

RUN curl -o /bin/wp https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar \
&& chmod +x /bin/wp \
Expand Down

0 comments on commit bea9559

Please sign in to comment.