I have the following task and as it is a little different from most setups, I couldn't find any hints that solved my problem.
WordPress for the live site was installed into a subfolder /wp1
, the .htaccess
directives are a rewrite to www and https followed by the usual WP directives:
<IfModule mod_rewrite.c>
RewriteEngine On
# rewrite any request to the certified domain to use www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# rewrite to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
and index.php
in the root loads the necessary wp1/wp-blog-header.php
.
Now I need to set up a staging environment in /wp2
with wp2
as the subdomain. The subdomain does not have a SSL-certificate (but I could easily get one). I tried several modifications in order to redirect the subdomain to the staging site without https, but all I got so far was errors.
Although I am not that familiar with .htaccess
, I think the first problem is the wildcard redirect to www. The second obstacle is the redirect to https. I need them both for production.
Is there a way to skip those rules in case the subdomain is addressed?
Or in general, how would you go about that?
I have the following task and as it is a little different from most setups, I couldn't find any hints that solved my problem.
WordPress for the live site was installed into a subfolder /wp1
, the .htaccess
directives are a rewrite to www and https followed by the usual WP directives:
<IfModule mod_rewrite.c>
RewriteEngine On
# rewrite any request to the certified domain to use www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# rewrite to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
and index.php
in the root loads the necessary wp1/wp-blog-header.php
.
Now I need to set up a staging environment in /wp2
with wp2
as the subdomain. The subdomain does not have a SSL-certificate (but I could easily get one). I tried several modifications in order to redirect the subdomain to the staging site without https, but all I got so far was errors.
Although I am not that familiar with .htaccess
, I think the first problem is the wildcard redirect to www. The second obstacle is the redirect to https. I need them both for production.
Is there a way to skip those rules in case the subdomain is addressed?
Or in general, how would you go about that?
Assuming the /wp2
subdirectory containing the staging site has its own WordPress .htaccess
file (with the standard WordPress front-controller, see below). Then the .htaccess
in the document root should contain something like the the following, above any existing directives, to rewrite all requests to the wp2
subdomain to the /wp2
subdirectory:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(wp2)\.example\.com
RewriteRule (.*) /%1/$1 [L]
Then, the "standard" WordPress front-controller would be in the /wp2/.htaccess
file (this also prevents a potential rewrite loop):
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Note that I have removed the RewriteBase
directive entirely and removed the slash prefix on the last RewriteRule
substitution. This is so that it will load index.php
from /wp2/index.php
and not the document root. (Which is really how I would expect the main site in /wp1
to be configured.)
/wp1
, the htaccess directives are the usual" - Although the "problem" with the "usual" directives in.htaccess
is that those directives are intended to work with a site installed in the root, not a subdirectory. "...and index.php in root loads the necessarywp1/wp-blog-header.php
" - Why are you doing it that way? – MrWhite Commented May 8, 2019 at 16:43wp2
subdomain to point directly to the/wp2
subdirectory, instead of the document root of the main site (which I assume is where it is pointing to now)? Then you wouldn't have to do anything. (I assume/wp1
features in the URLs for the live site?) – MrWhite Commented May 8, 2019 at 22:09