最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

htaccess - Redirect http to https does not work on subdir where another instance of WordPress installed

programmeradmin6浏览0评论

I have installed two instances of WordPress, on / and a subdirectory /

Now to make all HTTP URLs be redirected to the HTTPS version, I add the following directive in .htaccess at the root folder, as follows:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ /$1 [L,R=301,NC]

This works for all the URLs under the root folder, except those under /blogs/ subfolder.

I try to modify the .htaccess under /blogs/ by adding the above directive again. But that does not work. Why?

  1. I think the directive in `.htaccess is inherited by subfolder, why it does not work for /blogs/?
  2. Should I change the directive to:
RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ /$1 [L,R=301,NC]

Actually I have tried to do so, but not working either.

Update:

Now let me provides an example to explain this more clearly:

For URL such as /, it CAN be redirected to the https version / properly.

However, for URL under /blogs/ subfolder, such as , it CANNOT be redirected to the https version, instead, what you get is still the HTTP version. That is the problem.

Update

Below is the /blogs/.htaccess, to make things simple, I have disabled the WP Fastest Cache plugin:

# 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.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blogs/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blogs/index.php [L]
</IfModule>

# END WordPress

# Wordfence WAF
<Files ".user.ini">
<IfModule mod_authz_core.c>
    Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
    Order deny,allow
    Deny from all
</IfModule>
</Files>

# END Wordfence WAF

Update 2

I have tried to set Redirection plugin's site options for both the main site and the blog site, as described in / , ubt that still does not work, either.

I have installed two instances of WordPress, on https://www.example/ and a subdirectory https://www.example/blogs/

Now to make all HTTP URLs be redirected to the HTTPS version, I add the following directive in .htaccess at the root folder, as follows:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example/$1 [L,R=301,NC]

This works for all the URLs under the root folder, except those under /blogs/ subfolder.

I try to modify the .htaccess under /blogs/ by adding the above directive again. But that does not work. Why?

  1. I think the directive in `.htaccess is inherited by subfolder, why it does not work for /blogs/?
  2. Should I change the directive to:
RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example/blogs/$1 [L,R=301,NC]

Actually I have tried to do so, but not working either.

Update:

Now let me provides an example to explain this more clearly:

For URL such as http://www.example/outlook-repair/, it CAN be redirected to the https version https://www.example/outlook-repair/ properly.

However, for URL under /blogs/ subfolder, such as http://www.example/blogs/2-useful-methods-to-recover-outlook-data-from-a-damaged-disk-image-file, it CANNOT be redirected to the https version, instead, what you get is still the HTTP version. That is the problem.

Update

Below is the /blogs/.htaccess, to make things simple, I have disabled the WP Fastest Cache plugin:

# 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.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blogs/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blogs/index.php [L]
</IfModule>

# END WordPress

# Wordfence WAF
<Files ".user.ini">
<IfModule mod_authz_core.c>
    Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
    Order deny,allow
    Deny from all
</IfModule>
</Files>

# END Wordfence WAF

Update 2

I have tried to set Redirection plugin's site options for both the main site and the blog site, as described in https://redirection.me/support/site-options/ , ubt that still does not work, either.

Share Improve this question edited Sep 27, 2020 at 2:59 MrWhite 3,8911 gold badge20 silver badges23 bronze badges asked Sep 23, 2020 at 6:11 alanccalancc 416 bronze badges 2
  • Please include the contents of the /blog/.htaccess file in your question. – MrWhite Commented Sep 23, 2020 at 9:20
  • 1 @MrWhite, I have included the .htaccess, Thank you. – alancc Commented Sep 27, 2020 at 1:09
Add a comment  | 

1 Answer 1

Reset to default 1

I think the directive in .htaccess is inherited by subfolder

The mod_rewrite directives in the root .htaccess file are not inherited by the /blog/.htaccess file (by default). You would need to specifically enable mod_rewrite inheritance, however, this probably adds unnecessary complexity.

I try to modify the .htaccess under /blogs/ by adding the above directive again. But that does not work. Why?

Even if mod_rewrite inheritance was enabled this would not work unaltered since it would redirect requests back to the document root. You would need to have modified the directive like you mentioned (by adding the /blogs subdirectory in the substitution), however, this would have broken the redirect for the WordPress site in the document root.

Instead, you need to repeat the HTTP to HTTPS redirect at the top of the /blog/.htaccess file. And since this is in a subdirectory and the subdirectory would seem to be part of the URL-path, it would be preferable to use the REQUEST_URI server variable instead to avoid having to hardcode the subdirectory in the directive (although the subdirectory is hardcoded in later directives anyway - this could be avoided - but I assume is created by WordPress).

(Aside: If you did enable mod_rewrite inheritance you would have needed to have changed the root .htaccess file to also use REQUEST_URI instead. However, this is unlikely to be the only thing you would have needed to change.)

For example:

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^ https://www.example%{REQUEST_URI} [R=301,L]

The REQUEST_URI server variable contains the full, root-relative (starting with a slash) URL-path.

The NC flag is superfluous here.

发布评论

评论列表(0)

  1. 暂无评论