How to link subdirectories using subdomain? (VirtualHost, mod_rewrite, htaccess, DNS)

188 views Asked by At

I am using EC2 and Route 53 of AWS(amazon web service) and LAMP(Apache, MySQL, PHP) of Bitnami.

I am trying to run two wordpress sites independently:

 ...local_path/htdocs/subsite1
 ...local_path/htdocs/subsite2

I want to access these subsites using subdomain like following:

 subsite1.mydomain.com → ...local_path/htdocs/subsite1
 subsite2.mydomain.com → ...local_path/htdocs/subsite2

Moreover, I want to switch where "mydomain.com" and "www.mydomain.com" when I want to(maybe once or twice a year). Specifically, I want to switch between Case A and B when I need.

Case A :
 subsite1.mydomain.com -↘︎  
 mydomain.com ----------→ ...local_path/htdocs/subsite1
 www.mydomain.com ------↗︎

 subsite2.mydomain.com → ...local_path/htdocs/subsite2

Case B :
 subsite1.mydomain.com → ...local_path/htdocs/subsite1

 mydomain.com ----------↘︎ 
 www.mydomain.com ------→ ...local_path/htdocs/subsite2
 subsite2.mydomain.com -↗︎

I tried to combine DNS(AWS Route 53), VirtualHost, mod_rewrite and/or htaccess following several internet articles, but it was not successful, though I think there is several way to realize it.

What is the best way to resolve this, considering security and convenience? and How should I set them? Thank you in advance!

1

There are 1 answers

1
Hosik Chae On

(SELF-ANSWER)

I resolved it using VirtualHost.

Especially in the case of "mydomain.com" and "www.mydomain.com", I used ServerAlias.

First of all, I registered A records for my server's ip:

mydomain.com. A XXX.XXX.XXX.XXX
www.mydomain.com. A XXX.XXX.XXX.XXX
subdomain1.mydomain.com. A XXX.XXX.XXX.XXX
subdomain2.mydomain.com. A XXX.XXX.XXX.XXX

Second, in the config file for VirtualHost, (in my case, .../apache2/conf/extra/httpd-vhosts.conf)

NameVirtualHost *:80
<VirtualHost *:80>
        ServerAdmin [email protected]
        DocumentRoot ...local_path/htdocs/subsite.mydomain.com/htdocs
        ServerName subsite1.mydomain.com
        ServerAlias subsite1.mydomain.com mydomain.com
        ErrorLog "logs/subsite1.mydomain.com-error_log"
        CustomLog "logs/subsite1.mydomain.com-acccess_log" common
</VirtualHost>


<VirtualHost *:80>
        ServerAdmin [email protected]
        DocumentRoot ...local_path/htdocs/subsite2.mydomain.com/htdocs
        ServerName subsite2.mydomain.com
        ServerAlias subsite2.mydomain.com www.mydomain2.com
        ErrorLog "logs/subsite2.mydomain.com-error_log"
        CustomLog "logs/subsite2.mydomain.com-access_log" common
</VirtualHost>

It works great!

Reference (Koreaen articles about VirtualHost)