Page 1 of 1

mod_rewrite to rewrite HTTP_HOST (subdomains)

Posted: Tue Feb 27, 2007 11:32 am
by Luke
I'm trying to use mod_rewrite to send users to a sub-directory for certain sub-domains. Is there a better way to do this (w/out mod_rewrite)? If not, than how do I rewrite the HTTP_HOST portion of the uri?

Posted: Tue Feb 27, 2007 1:02 pm
by Kieran Huggins
sounds like a job for the httpd.conf to me.. I forget what section though. maybe define a virtualhost?

Posted: Tue Feb 27, 2007 1:11 pm
by Luke
i don't have access to it.

Posted: Tue Feb 27, 2007 2:33 pm
by Luke
OK, so this rule works:

Code: Select all

RewriteEngine On
# Skip www.mysite.info
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.mysite\.info
RewriteRule ^/(.*)$  /vservers/mysite/htdocs/%1/$1 [L]
But it attaches /blog to the end of the URI in the URL, so I type in blog.mysite.info, and then it redirects me to http://blog.mysite.info/blog. Is there any way to make it not show the /blog part?

Posted: Fri Mar 02, 2007 3:12 pm
by Luke
Image

Posted: Fri Mar 02, 2007 3:49 pm
by nickvd
Are you looking for an automated solution? or is this for your own personal usage.

I say this because most hosts will provide a subdomain manager in your hosting control panel which you can define the domain and the folder it points to.

Posted: Fri Mar 02, 2007 4:16 pm
by Luke
My host configured the dns so the subdomains would work, but they told me that that's as much as they will do. They said to write a "script" that redirects the user, as in like a PHP script in the index.php... which I find pretty impractical.

Posted: Fri Mar 02, 2007 4:32 pm
by Luke
Nevermind... this works.

Code: Select all

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(wiki|blog)\.mc2design\.info
RewriteRule ^(.*)$  %1/$1 [L,QSA]
EDIT: now... anybody have any ideas on how to keep requests from http://www.mc2design.info/wiki and http://www.mc2design.info/blog from resolving? I only want those to be accessible via the subdomain. I can't figure out how to make it work. :(

Posted: Fri Mar 02, 2007 4:40 pm
by nickvd
Surely your host provides a control panel of some sort. (cpanel for example)

I.e.: http://jefs.com/nick/Clipboard01.jpg

Posted: Fri Mar 02, 2007 4:54 pm
by Luke
they give you the ability to add sub-domains to point to an IP, but not to a directory. :(

Posted: Fri Mar 02, 2007 5:02 pm
by nickvd
Doh :(

Posted: Wed Mar 07, 2007 12:06 pm
by Luke
Alright, I've got another question related to this...

I've got a subdomain set up at blog.mc2design.com and a subdomain set up at blog.mc2design.info. If blog.mc2design.com is pointed at mc2design.com, what would I have to do in order for somebody to type in blog.mc2design.com and have them resolve to blog.mc2design.info, but never know about it? Is it possible? I've never been able to get this to work.

Posted: Wed Mar 07, 2007 12:48 pm
by nickvd
So, you want both .info and .com to resolve to the same ip address, so that all requests get sent to the same server?

Add a new entry in your dns system for .info pointing to the same ip address, you will have to ensure that you can add a domain to your hosting account (some hosts charge per month for this).

Other than that, I can't think of anything else you'd be required to do.

Posted: Wed Mar 07, 2007 1:23 pm
by Luke
Tell me if this resolves for you:
http://blog.mc2design.com/