1

I have installed two instances of WordPress, on https://www.example.com/ and a subdirectory https://www.example.com/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.com/$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.com/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.com/outlook-repair/, it CAN be redirected to the https version https://www.example.com/outlook-repair/ properly.

However, for URL under /blogs/ subfolder, such as http://www.example.com/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.

2
  • Please include the contents of the /blog/.htaccess file in your question.
    – MrWhite
    Sep 23 '20 at 9:20
  • 1
    @MrWhite, I have included the .htaccess, Thank you.
    – alancc
    Sep 27 '20 at 1:09
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.com%{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.

1
  • 1
    Thank you very much.
    – alancc
    Sep 27 '20 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.