In professional shared hosting, many a times user directory is redirected towards a subdomain. They usually acheive it via some scripting langugage like Perl, PHP with or without MySQL like database. However, we can use just Apache module mod_rewrite
‘s dynamic mass virtual hosts feature to acheive near same result. Here is how to automatically redirect subdirectory to domain with Apache 2.4.
Automatically Redirect Subdirectory to Domain
First, it is important to note that we are using non-HTTPS sites for this guide. Second, we need a cloud based DNS service who supports wildcard entry. If we have content of www.abhishek.thecustomizewindows.com
at /var/abhishek/www
sub-directory path, then the system will redirect to www.abhishek.thecustomizewindows.com
. If second entry is www.ghosh.thecustomizewindows.com
at /var/ghosh/www
sub-directory path, then the system will redirect to www.ghosh.thecustomizewindows.com
. It is like permalink rewriting but with subdomain. As it is with subdomain, we need the reliability of DNS for faster resolution. In our example, *.thecustomizewindows.com
will point to either main server IP or alternate IP. Easiest example of that configuration is :
1 2 3 4 | RewriteEngine on RewriteMap lowercase int:tolower RewriteCond %{lowercase:%{HTTP_HOST}} ^www\.([^.]+)\.thecustomizewindows\.com$ RewriteRule ^(.*) /var/%1/www$1 |
tolower
directive is used to make sure that the hostnames used will be all lowercase to avoid ambiguity in the directory structure. We are adding the stanza on virtual host file. Apache official site has good documentation on dynamic mass virtual hosts :
---
1 | https://httpd.apache.org/docs/2.4/vhosts/mass.html |
We can use the directives of Apache Module mod_vhost_alias :
1 | https://httpd.apache.org/docs/2.4/mod/mod_vhost_alias.html |
We can use a separate configuration file named vhost.map
with content like this to point which subdomain will point where:
1 2 3 4 | customer-1.thecustomizewindows.com /www/customers/1 customer-2.thecustomizewindows.com /www/customers/2 # ... customer-N.thecustomizewindows.com /www/customers/N |
In that case, our main configuration will be like following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | RewriteEngine on RewriteMap lowercase int:tolower # define the map file RewriteMap vhost txt:/www/conf/vhost.map # deal with aliases as above RewriteCond %{REQUEST_URI} !^/icons/ RewriteCond %{REQUEST_URI} !^/cgi-bin/ RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$ # this does the file-based remap RewriteCond ${vhost:%1} ^(/.*)$ RewriteRule ^/(.*)$ %1/docs/$1 RewriteCond %{REQUEST_URI} ^/cgi-bin/ RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$ RewriteCond ${vhost:%1} ^(/.*)$ RewriteRule ^/(.*)$ %1/cgi-bin/$1 [H=cgi-script] |
There are complex ways of achieving the result, such as using RewriteMap Directive with some scripting :
1 | https://httpd.apache.org/docs/2.4/vhosts/mass.html |
Hope it will help.
Tagged With apache2 domain with subdirectory , redirect domain sub directory to another domain subdirectory