[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do i point a subdomain to a subdirectory?

Posted on 2008-10-11
8
Medium Priority
?
437 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 1800 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 200 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

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
 

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 1800 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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses
Course of the Month19 days, 14 hours left to enroll

873 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