Redirecting a subdomain to a url or directories in apache

By grossiweb9/14/2015
articles

You can check this by running:

ping hello.domain.com
This should respond with something like this:
[root@grossiweb conf]# ping hello.domain.com
code>PING hello.domain.com (62.193.xxx.xxx) 56(84) bytes of data.
64 bytes from my.serverhostname.com (62.193.xxx.xxx): icmp_seq=0 ttl=64 time=0.058 ms
64 bytes from my.serverhostname.com (62.193.xxx.xxx): icmp_seq=1 ttl=64 time=0.040 ms
64 bytes from my.serverhostname.com (62.193.xxx.xxx): icmp_seq=2 ttl=64 time=0.071 ms
64 bytes from my.serverhostname.com (62.193.xxx.xxx): icmp_seq=3 ttl=64 time=0.028 ms
The next step is to set-up the virtual host to respond to the wildcard subdomains. In the case of plesk you have to set-up a vhost.conf file but on any other apache installation you can just edit httpd.conf and look for the that corresponds to your site. If you are using Plesk you will need to ssh in to your server and create a vhost.conf file if it doesn’t already exist here where is your domain:
/var/www/vhosts//conf/vhost.conf
Open that file with the command line editor of your choice and add the following directives:
ServerAlias *.domain.com
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.* [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com
RewriteCond /var/www/vhosts/domain.com/httpdocs/%1 -d
RewriteRule ^(.*) /%1/$1 [L]
Substitute domain.com for your domain. Now here’s a quick walkthrough how these rules work. The first line ServerAlias *.domain.com tells apache to accept anysubdomain.domain.com.
Next the rewrite rules work as follows:
  1. The first line of mod_rewrite !^www.* only matches urls that don’t begin with www. You don’t want to redirect www.domain.com to www.domain.com/www/ do you? Note: you could add additional lines like these to prevent re-writing other subdomains like secure.domain.com if that should not be redirected to domain.com/secure/
  2. The second line ^([^\.]+)\.*$ captures into a backreference anything that doesn’t have a period e.g. the subdomain. The plus makes sure that there is at least one character that matches.
  3. The next condition checked is that the directory captured into the backreference does actually exist hence the -d.
  4. This last line ^(.*) /%1/$1 [L] takes the requested path and dumps it into a back reference and then rewrites it to point to the a directory (the subdomain backreference captured in the Rewrite conditions above) . In other words %1 is the subdomain backreference and $1 is the path backreference (that captured by .*). The important thing to rember is you can refer to backreferences in rewriteCond lines with % and backreferences in rewriteRules with $. Lastly the [L] (last) means doen’t further rewrite this URL. Note: You might want to leave this out if you are doing any subsequent rewrites, be sure to test what you have set-up thoroughly!!!
Now once you have made those changes if you are running plesk you will need to apply your vhost.conf changes with the following command:
/usr/local/psa/admin/sbin/websrvmng --reconfigure-vhost --vhost-name=
And then whether are using Plesk or not you need to restart apache e.g:
service httpd restart
Now you should find that if you set-up a directory called “coolio” with a valid index.html file in it. coolio.domain.com should find that file.

We focus on aspects that matter to your businessThat's what makes our clients happy

Rhia

ATHENS LANGUAGE SCHOOLHOUSE

I am fortunate to have found Grossi Web to construct my website and help me realize my marketing strategy and look! Grossi Web listened to my ideas and created the perfect end result. The process was made easy for me allowing me creative freedom without stress. Grossi Web worked fast, producing a professional and stunning website. I am so pleased with Grossi Web!

Thay

HUMES MCCOY AVIATION

Thanks to the Talent and Professionalism of GrossiWeb, I have turned a dream into a successful aviation company. I continuously receive compliments on the corporate site and AirExpress® brand. GrossiWeb developed a highly targeted marketing campaign that has resulted in international exposure and attaining clients who were previously unreachable!

Bob

DORFMAN CAPITAL

I had been courting a high profile business prospect, but after a few months my prospect was becoming increasingly distant. One morning after I had made some follow up calls, my prospect called me right back stating he had seen a news story about my company on Yahoo news. This is the type of instant gratification and credibility I have experienced working with Grossi Web.

Rhia

ATHENS LANGUAGE SCHOOLHOUSE

I am fortunate to have found Grossi Web to construct my website and help me realize my marketing strategy and look! Grossi Web listened to my ideas and created the perfect end result. The process was made easy for me allowing me creative freedom without stress. Grossi Web worked fast, producing a professional and stunning website. I am so pleased with Grossi Web!

Thay

HUMES MCCOY AVIATION

Thanks to the Talent and Professionalism of GrossiWeb, I have turned a dream into a successful aviation company. I continuously receive compliments on the corporate site and AirExpress® brand. GrossiWeb developed a highly targeted marketing campaign that has resulted in international exposure and attaining clients who were previously unreachable!

Bob

DORFMAN CAPITAL

I had been courting a high profile business prospect, but after a few months my prospect was becoming increasingly distant. One morning after I had made some follow up calls, my prospect called me right back stating he had seen a news story about my company on Yahoo news. This is the type of instant gratification and credibility I have experienced working with Grossi Web.

What happens after you contact us?

15 minutes consultation

We will schedule a quick 15-minute call with you to understand what you need to unlock the full potential of your business.

Project Proposal

After we got every output from you, we will prepare a proposal for you. If you agree with our pricing and timeline, we are all set to take off.