Pointing Subdomains to subdirectories with .htaccess

I know there are thousands of threads about this topic, but despite many hours of searc and try, I’m really unable to find a solution to my case. So I decided to ask for help.

This is my panorama

Read More

1) I have a domain www.mydomain.com with a WordPress installation and Wildcard SSL certificate

As you probably know, WordPress already writes some rules in .htaccess, these rules:

# 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>

2) now I need to put additional wordpress installations (no WPMU, but different installations) in other subdomains, pointing them to subdirectories that I would like to call “subdomain_NAMEOFTHESUBDOMAIN”

3) as far as I know, to extend the SSL certificate I cannot create subdomains via cPanel, but I have to do it with .htaccess rules

This is where I’ve been able to get:

1) I’ve created a wildcard CNAME *.mydomain.com

2) I’ve created a directory inside my public_html folder, called “subdomain_xxx”

3) I’ve added these rules (before the wordpress rules) to point the subdomain “xxx.mydomain.com” to the “subdomain_xxx” folder

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^subdomain_xxx/ - [L]
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RewriteCond %{HTTP_HOST} ^([^.]+).mydomain.com$
RewriteRule ^(.*)$ subdomain_xxx/$1 [L]
</IfModule>

At first it seems to work:

  • if I go to mydomain.com I get the corretc “first level” wordpress www.mydomain.com
  • if I go to ANYTHING.mydomain.com I still get www.mydomain.com
  • if I go to xxx.mydomain.com I’m correctly pointed to the wordpress installation present in the “subdomain_xxx” directory

but there is a problem:

If I try to duplicate the procedure for any other htaccess-subdomain/subdirectory couple,
for example “subdomain_yyy” and “yyy.mydomain.com”, duplicating the previous rules:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^subdomain_yyy/ - [L]
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RewriteCond %{HTTP_HOST} ^([^.]+).mydomain.com$
RewriteRule ^(.*)$ subdomain_yyy/$1 [L]
</IfModule>

I keep being redirected to the first subdomain created: xxx.mydomain.com

To be very clear, this is my full .htaccess content:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteRule ^subdomain_xxx/ - [L]
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RewriteCond %{HTTP_HOST} ^([^.]+).mydomain.com$
RewriteRule ^(.*)$ subdomain_xxx/$1 [L]

RewriteRule ^subdomain_yyy/ - [L]
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RewriteCond %{HTTP_HOST} ^([^.]+).mydomain.com$
RewriteRule ^(.*)$ subdomain_yyy/$1 [L]
</IfModule>

# 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>

Surely there’s a lot I do not understand about .htaccess (and regex), so I’m not only looking for a quick solution, but also some other infos to deepen my comprehension of this matter.
I hope someone will find the patience for that.

Thanks in advance

Related posts

Leave a Reply

1 comment

  1. I just found out how to correctly do it

    The rules I was using were all wrong.

    this is the correct way

    RewriteCond %{HTTP_HOST} ^xxx.mydomain.com$ [NC]
    RewriteRule ^(.*)$ /subdomain_xxx/$1 [L]
    

    If someone has a comment I will be glad to read it, thanks anyway