Solved

How do i point a subdomain to a subdirectory?

Posted on 2008-10-11
8
413 Views
Last Modified: 2013-11-22
My objective is to find out how to point a subdomain to an existing domain subdirectory.

Currently  www.domainname.com points to htdocs/domainname/ as the root directory

I created a subdomain,  subd.domainname.com by creating an A record for subd.domainname.com pointing to the IP address for domainname.com

I created a subdirectory  htdocs/domainname/subd and I want the subdomain subd.domainname.com to point to the subdirectory (but still show subd.domainname.com in the address bar)

The host operator says I must use MOD_rewrite to do this.

The current setup uses a Virtual Private Server running FreeBSD and Apache Web Server with mod_rewite enabled. the httpd.conf file points www.domainname.com and domainname.com to the document root htdocs/domainname/

I would greatly appreciate keeping in mind that not only am I at a novice level, I have cognitive deficits. [You would be surprised how good web related projects are as therapy; they teach and reinforce logical step-by-step thinking. For someone such as myself, who was once a highly educated professional,  learning to do this is a challenge. I try to find out first what to do, then learn from doing it. Thank you in advance.]
0
Comment
Question by:Brokenstick
  • 4
  • 2
  • 2
8 Comments
 
LVL 27

Assisted Solution

by:caterham_www
caterham_www earned 450 total points
ID: 22696737
It looks like you have access to the httpd.conf file. In that case it's better to make changes there.

One possible solution would be:
Create an additional <virtualhost...> container/dupliacte the existent one but now with ServerName being set to your subdomain and adjust the document root to the subfolder.

The other way is to use mod_rewrite within the existent virtual host section. You need a rule which checks the HTTP_HOST
RewriteEngine on

RewriteCond %{HTTP_HOST} ^subd\.domainname\.com

RewriteCond $1 !^subd

RewriteRule ^/(.*) /subd/$1 [L]

Open in new window

0
 

Author Comment

by:Brokenstick
ID: 22698613
Thank you, Caterham. I would like to give this a try, so please indulge me for a bit. This is not as simple for me as it probably should be.

Since I do have access to the httpd.conf files, with respect to your first suggestion,

"Create an additional container/dupliacte the existent one but now with ServerName being set to your subdomain and adjust the document root to the subfolder."

I have two questions:

(1)  Do I understand correctly that if I duplicate the section in the httpd.conf file for the original domain.com,

<VirtualHost domainname.com www.domainname.com>
  ServerName www.domainname.com
  ServerAdmin webmaster@domainname.com
  DocumentRoot /usr/local/etc/httpd/htdocs/domainname
</VirtualHost>

Would I change the DUPLICATE to read as follows:

<VirtualHost domainname.com www.domainname.com>
  ServerName subd.domainname.com
  ServerAdmin webmaster@domainname.com
  DocumentRoot /usr/local/etc/httpd/htdocs/domainname/subd
</VirtualHost>

(Is this necessary to know: do I change the top line from domain.com www.domain.com also?  If so, would it be to just subd.domainname.com?)

(2) Do I need to make other changes elsewhere -- for example, to the Zone Record?

With respect to the MOD_rewrite alternative, I know this is really silly but: where does the code go? You stated: "within the existent virtual host section"  -in context it looks like that would also be in the httpd.conf file as well, but I am not 100% certain.  And if it DOES belong in the httpd.conf file, where would it go?


Would I take the ORIGINAL VirtyualHost entry (above) for domainname.com  www.domainname.com
and add your code, such as:

<VirtualHost domain.com www.domain.com>
  ServerName www.domain.com
  ServerAdmin webmaster@domain.com
  DocumentRoot /usr/local/etc/httpd/htdocs/domain
  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^subd\.domainname\.com
  RewriteCond $1 !^subd
  RewriteRule ^/(.*) /subd/$1 [L]
</VirtualHost>

Thank you again.  I looked up the function of RewriteCond and RewriteRule and it helped me udnerstand how that part "works", but putting it all together is still daunting to me.

Brokenstick

0
 
LVL 10

Assisted Solution

by:srgilani
srgilani earned 50 total points
ID: 22700159
<<             Wrong Entry
<VirtualHost domainname.com www.domainname.com>
 ServerName subd.domainname.com
 ServerAdmin webmaster@domainname.com
 DocumentRoot /usr/local/etc/httpd/htdocs/domainname/subd
</VirtualHost>

>>

<<  Correct Entry

<VirtualHost subd.domainname.com>
 ServerName subd.domainname.com
 ServerAdmin webmaster@domainname.com
 DocumentRoot /usr/local/etc/httpd/htdocs/domainname/subd
</VirtualHost>

>>
0
 

Author Comment

by:Brokenstick
ID: 22700475
Okay experts, thank you.

I have made the changes to the httpd.conf in accordance with both suggestions combined.

However, when entering http://subd.domainname.com in a web browser, Firefox returns "Page Load Error - Address Not Found"; E7 returns "Internet Explorer cannot display the webpage"

I suspect that the problem is probably with the zone record?

Initially, I had created an entry in the domainname.com zone record:

subdr.domainname.com IN A xxx.xxx.xxx.xxx

where the IP Address is the same as the IP Address for domainname.com

Giving it some thought, it seems to me that subd.domainname.com is not unlike the entry for www.domainname.com; so I dropped the IN A record and added IN CNAME:

subd.domainname.com IN CNAME domainname.com.

I will wait a bit until that propigates and check again.

If you have any other suggestions, I will appreciate hearing from you all.


0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:Brokenstick
ID: 22704176
I must be really THICK.

I would like to know if I may split points and if so how to do it. I tried finding it and couldn't. So then I thought maybe I would send a message to a "Community Advisor."  I found many of them, but try as I might thirty ways to Sunday I could not find out how to send a message. If you click on any of the advisors, all I get is a profile. Period.

So I am sure I am missing something. I want to thank and reward BOTH of the individuals who helped me because they BOTH helped.

But I haven't a clue. <LOL>
0
 

Author Comment

by:Brokenstick
ID: 22704485
Almost a complete success!

            The zone record has propagated and subd.domainname.com correctly displays the content (index) page of the subdirectory htdocs/domainname/subd.

             The "however" is that the url also changes, and instead of displaying
http://subd.domainname.com/, the address bar displays http://www.domainname.com/subd/ instead.

              Did I do something wrong, or do I just need some additional code to ad the eyeliner to this pig's lipstick?
0
 
LVL 27

Accepted Solution

by:
caterham_www earned 450 total points
ID: 22705724
Your virtual host configuration looks like an IP-based virtual host one (=one IP per each IP-based virtual host, http://httpd.apache.org/docs/2.2/vhosts/ip-based.html ).

For name-based virtual hosting (one IP per multiple virtual hosts, http://httpd.apache.org/docs/trunk/vhosts/name-based.html ), you must use the NameVirtualHost direktive. The matching is based on the HTTP_HOST header of a HTTP request and the ServerName/ServerAlias directives.
NameVirtualHost *:80

<VirtualHost *:80>

 ServerName www.domainname.com

 ServerAlias domainname.com

 ServerAdmin webmaster@domainname.com

 DocumentRoot /usr/local/etc/httpd/htdocs/domainname

</VirtualHost>
 

<VirtualHost *:80>

 ServerName subd.domainname.com

 ServerAlias www.subd.domainname.com

 ServerAdmin webmaster@domainname.com

 DocumentRoot /usr/local/etc/httpd/htdocs/domainname/subd

</VirtualHost>

Open in new window

0
 
LVL 10

Expert Comment

by:srgilani
ID: 22708423
I am sure your problem has been solve after above post of caterham_www.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now