6afe967d885bf97b69642b5e4ac3ef4f288975ef
By:
shalintj

Redirect rules in .htaccess for multiple Wordpress installation

July 4, 2017 598 views
WordPress Apache Ubuntu 16.04

I currently have 2 Wordpress installations as:

https://www.example.com
https://www.example.com/subdirectory

The .htaccess for https://www.example.com has the following rules:

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

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

And the .htaccess of https://www.example.com/subdirectory looks like this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

How do I force https re-direct as per the following:

  1. http://www.example.com/subdirectory to https://www.example.com/subdirectory
  2. http://example.com/subdirectory to https://www.example.com/subdirectory
  3. https://example.com/subdirectory to https://www.example.com/subdirectory
1 Answer
hansen July 4, 2017
Accepted Answer

Hi @shalintj

Simply just add the following to the top of the .htaccess of /subdirectory:

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

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

  • Tried that. It redirects to the root folder then. I mean if I add the rule that you mentioned in the sub-directory's .htaccess, this is what happens:

    https://www.example.com/subdirectory to https://www.example.com

    • That's strange, it should use the full URL in the regular expression. Try this:

      RewriteEngine on
      RewriteCond %{HTTP_HOST} ^example.com [NC]
      RewriteRule ^(.*)$ https://www.example.com/subdirectory/$1 [L,R=301,NC]
      
      RewriteEngine On
      RewriteCond %{HTTP:X-Forwarded-Proto} !https
      RewriteRule ^(.*)$ https://%{HTTP_HOST}/subdirectory/$1 [R=301,L]
      
      • It worked. Thanks a lot.

        Why do you think the earlier one didn't work?

        • I'm not sure. I haven't used Apache for several years. Nginx is just easier to figure out, but takes a little time to get used to if you have used Apache for a long time.
          My guess would be that it might have something to do with RewriteBase, but that's required for the other WordPress rewrites to work - otherwise those needs to be changed, so it's tomato-tomato.

Have another answer? Share your knowledge.