directory - htaccess, site and staging in subdirectories

admin2025-01-07  6

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?

Share Improve this question edited May 9, 2019 at 18:43 MrWhite 3,8911 gold badge20 silver badges23 bronze badges asked May 8, 2019 at 15:49 modusmodus 93 bronze badges 6
  • "WordPress for the live site was installed into a subfolder /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 necessary wp1/wp-blog-header.php" - Why are you doing it that way? – MrWhite Commented May 8, 2019 at 16:43
  • Actually I don't know, it has been done that way in 2011, because there was another cms already installed in root. It is method 2 given in the Codex: link. I'm not sure what you are hinting at? – modus Commented May 8, 2019 at 16:54
  • Can you not configure your wp2 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
  • Concerning the subdomain: not by myself, thanks for the hint. Concerning the URLs: the subdirectory isn't part of them. – modus Commented May 9, 2019 at 10:44
  • I decided the problem is solved by not using a subdomain, not having to poke around in the htaccess of a live site. Putting "ErrorDocument 401 default" into root htaccess solved the problem of 401 errors showing up and I hope I can continue to work, now. Thanks for all the help. – modus Commented May 9, 2019 at 11:08
 |  Show 1 more comment

1 Answer 1

Reset to default 0

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.)

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736263878a924.html

最新回复(0)