Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Gurus of So

I have a simple .htaccess file where I only redirect non www to www for my webapp. Now what I want to do is

  1. Create a new vhost like http://subdomain.mydomain.tld
  2. Create a redirect for anybody coming to that subdomain to a folder on my app like app/webroot/subdomain

My current .htaccess looks like this

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www\.mydomain\.tld [NC]
    RewriteRule ^(.*)$ http://www.mydomain.tld/$1 [R=301.L]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ app/webroot/$1 [QSA,L]
</IfModule>

How would I go about it? Can I do this all in my .htaccess file or do I need to mess with GoDaddy as well? I rather not do this in the app, if that makes sense.

I am running Ubuntu 10.04 + Apache 2.2.14-5

Thanks

share|improve this question

migrated from stackoverflow.com Sep 28 '11 at 3:32

This question came from our site for professional and enthusiast programmers.

1 Answer 1

up vote 18 down vote accepted

Here's what you need to put in your .htaccess file:

RewriteEngine on
# redirect mydomain.tld to www.mydomain.tld
RewriteCond %{HTTP_HOST} ^mydomain\.tld [NC]
RewriteRule ^(.*)$ http://www.mydomain.tld/$1 [R=301,L]

# default website
RewriteCond %{HTTP_HOST} ^www\.mydomain\.tld$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app/webroot/$1 [NC,QSA,L]

# subdomain to folder
RewriteCond %{HTTP_HOST} ^(sub|subfolder)\.domain\.com$ [NC]
RewriteCond %1 !^www$ [NC]
RewriteRule ^(.*)/?$ http://www.domain.com/%1/$1 [NC,QSA,L]
share|improve this answer
    
Thank you, Do I need to setup a vhost first for that subdomain? –  iUsable Sep 27 '11 at 10:46
    
You can always a vhost for it with the same DocumentRoot as the WWW if you want. You can also change the ServerAlias of your www vhost to add more domains eg: ServerAlias domain.com www.domain.com sub.domain.com. –  Book Of Zeus Sep 27 '11 at 10:50
    
Sorry, am a n00b with apache2. So, 2 things - 1. I want to only allow for a fixed subdomain and not ([a-z0-9]) as per your code, should I just change that to ^(sub)? 2. How do I set the vhost up again (too n00b for you? sorry)? –  iUsable Sep 27 '11 at 10:57
    
1) view my updated answer. 2) Did you create a sub domain entry? Did you point it to the same place as the www? –  Book Of Zeus Sep 27 '11 at 11:03
    
1) I am staring at it & can't find a single change yet, am I seriously lost? 2)No nothing else in that .htaccess file but what I have shown. –  iUsable Sep 27 '11 at 11:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.