• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 447
  • Last Modified:

How do i point a subdomain to a subdirectory?

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
Brokenstick
Asked:
Brokenstick
  • 4
  • 2
  • 2
3 Solutions
 
caterham_wwwCommented:
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
 
BrokenstickAuthor Commented:
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
 
srgilaniCommented:
<<             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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
BrokenstickAuthor Commented:
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
 
BrokenstickAuthor Commented:
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
 
BrokenstickAuthor Commented:
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
 
caterham_wwwCommented:
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
 
srgilaniCommented:
I am sure your problem has been solve after above post of caterham_www.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now