.htaccess redirect

asuh

Golden Member
Sep 7, 2002
1,059
2
91
Here's what I want to do.

I want to turn http://www.mywebsite.com/folder/subdomain/index.html into http://subdomain.mywebsite.com.

So, basically this means there's a folder in a subdirectory of my root that I want to use as a root for a subdomain. So let me use a real website just to be very clear.

http://www.microsoft.com/windows/longhorn/index.html
http://longhorn.windows.com

I want the second, not the first to show up in the address bar. My host uses cpanel for the administration and I can easily add a subdomain like longhorn.windows.com. And, when I try to point my browser in that direction by typing it into the address bar, it loads the site and the address bar then appears similar to www.microsoft.com/windows/longhorn.

When I create a subdomain from cpanel, it then creates a folder off of the root public_html. So, for example, let's say I create longhorn.microsoft.com. It will then create a folder called longhorn that you can see from the root of my domain. Must this folder absolutely exist? Because, I want to redirect the domain longhorn.microsoft.com to another folder located here public_html/windows/longhorn/. This being said, it'd seem redundant to have two folders called longhorn and only one of them to be used.

I know this requires some kind of .htaccess lines but I'm not sure how to do it. As well, when I do create the file with the correct code, where does the file need to be? In the public_html directory or the actual subdirectory?

Any answers would be veeeerrrrrrryy appreciated!!
 

jfall

Diamond Member
Oct 31, 2000
5,975
2
0
it'd be easiest to ask your webhost to change your doc root for subdomain.yoursite.com to public_html/subdomain
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
You could also just create a symlink between the two folders...but I dont see why you need the content to be in another sub directory.
 

asuh

Golden Member
Sep 7, 2002
1,059
2
91
The reason I want the content to be another sub directory is because I'm a web designer. Say, for instance, I am working with a client from Intel. So, I have a sub directory setup that can be accessed like www.mydomain.com/webdesign/intel. I would like for anyone at Intel who needs to see my design in progress to see intel.mydomain.com instead of the previous. It just looks more professional that way. I also can keep all of my web designs in one folder called webdesign.

MCrusty: What is a symlink?

jfall: I'm not sure how to ask my webhost about the doc root. I'm not even sure what that is.
 

jfall

Diamond Member
Oct 31, 2000
5,975
2
0
By doc root I mean document root, it's where Apache looks for files relating to your subdomain. You just need to have your webhost point it to the location of your files
 

asuh

Golden Member
Sep 7, 2002
1,059
2
91
So jfall, if I decide to use a lot of subdomains, won't they have to do this for every subdomain I create? That sounds more like a hassle, especially if lots and lots of people are doing that all the time.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
If you have ssh access to your webserver you would create a symlink like follows.

ln -s public_html/windows/longhorn/ public_html/longhorn

So whenever somebody accesses public_html/longhorn they are in fact accessing public_html/windows/longhorn

That way, nobody needs to mess with the doc root, and cPanel works with the default settings.
 

asuh

Golden Member
Sep 7, 2002
1,059
2
91
unfortunately, I do not have ssh access. They won't allow it for some reason, though I can't remember why.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
So jfall, if I decide to use a lot of subdomains, won't they have to do this for every subdomain I create? That sounds more like a hassle, especially if lots and lots of people are doing that all the time.

Well for blah.somedomain.com to work at all it would need a valid DNS entry and a VirtualHost entry in Apache, not exactly things you can do with a .htaccess file.
 

asuh

Golden Member
Sep 7, 2002
1,059
2
91
Okay, this is messed up. I contacted my webhost about this subdomain mess and they told me one of two things. I could either use an .htaccess file or open up another whole account in the name of the subdomain. The second option seems crazy, but the first one seems logical. I can't believe they wouldn't give me more help, but for whatever reason they won't. So, it's gotta be possible or I'm gonna be lost!
 

imported_waldo

Golden Member
Aug 30, 2004
1,076
0
0
It shouldn't be that difficult...as far as I know, you just have to set up the root directory to be where the index file is, create the subdomain, and that should be it...it may take a few days for the DNS servers to role over though.
 

asuh

Golden Member
Sep 7, 2002
1,059
2
91
Is there an easy way to do this using cpanel? I don't understand why this is so complicated. Shouldn't I just be able to create a subdomain in cpanel with it automatically using the subdomain address in the address bar instead of pointing to the actual location?

I looked at that apache link and it's too technical for me. Where do you change the setup in the core server configuration?

Just as well, if I have to point some DNS servers, how would I do that locally through my cpanel? Or is this on the server administration side?
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Where do you change the setup in the core server configuration?

In httpd.conf, which unless you have your own copy of apache is global to the machine it's on.
 

asuh

Golden Member
Sep 7, 2002
1,059
2
91
why won't something like this work?

RewriteEngine on

RewriteCond %{HTTP_HOST} ^subdomain.website.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.subdomain.website.com$
RewriteRule ^(.*)$ http://website.com/subdomain/$1 [R=301,L]

I tried this in a .htaccess file but it didn't work.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
You still need an Apache VirtualHost definition for it to recognize the domain, otherwise it will probably fall back on the default VirtualHost which is most likely your provider's web page.
 

asuh

Golden Member
Sep 7, 2002
1,059
2
91
so basically, unless I get access to my Apache server, I'm pretty much gonna have to live with what I have? I find that frustrating! What's the point of a subdomain if it won't keep the display of the subdomain in the address bar? I think it's so much more professional to be able to do that.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
What's the point of a subdomain if it won't keep the display of the subdomain in the address bar? I think it's so much more professional to be able to do that.

A subdomain has a lot more uses than just web space. And Apache will leave the name of the subdomain in the address bar, but you have to be able to properly configure Apache to handle that VirtualHost properly.
 

asuh

Golden Member
Sep 7, 2002
1,059
2
91
Hey Nothinman, it turns out you were exactly right on what you were saying, but I didn't completely understand why until someone else answered the question on another forum. Here's a link to what I discovered, posted here in case anyone else has the same trouble or questions.

.htaccess and subdomains