Welcome to WebmasterWorld Guest from 18.208.159.25

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

virtual subdomain with mod_rewrite

     
8:49 am on Sep 1, 2003 (gmt 0)

New User

10+ Year Member

joined:Sept 1, 2003
posts:30
votes: 0


I have only access to .htaccess and I want to create virtual subdomains.

I own a blog site and how do I create a .htaccess file so that [user.example.com...] will redirect to [example.com...]

Please note that [user.example.com...] does not exist.

Any help would be greatly appreciated.

Thanks in advance!

[edited by: DaveAtIFG at 5:50 pm (utc) on Sep. 1, 2003]
[edit reason] URLs edited [/edit]

3:22 pm on Sept 1, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


ebloggy,

Welcome to WebmasterWorld [webmasterworld.com]!

First of all, you'll need to have "wild-card" DNS enabled. With this setup, <anything>.yourdomain.com will be pointed to your server's IP address. This might be hard, or it might be easy - contact your domain registrar.

The rewrite itself is an easy one; I'd suggest you start with this Introduction to mod_rewrite [webmasterworld.com] thread, and follow the links in it. Having an understanding of the method will help you to avoid creating major problems for yourself as your site grows.

Please note that posting your real domain name is not allowed on WebmasterWorld; Please see the Terms Of Service (TOS) link below. The purpose is to prevent a flood of "Drive-by URL dropping" - to keep WebmasterWorld useful.

Jim

7:28 am on Sept 2, 2003 (gmt 0)

New User

10+ Year Member

joined:Sept 1, 2003
posts:30
votes: 0


Thanks I'm sorry about that post.

I checked my Control Panel (HSPHERE) and found that there's already the DNS A Record added in, *.mydomain.com to my shared server IP.

However, whenever I type gibberish.mydomain.com, it wouldn't lead to my main page, but my host's page.

Try it, use anything.mydomain.com (mydomain with my actual domain) and it wouldn't go to the www site.

2:33 pm on Sept 2, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Look around in the control panel for a place to define subdomains. I don't use a CPanel myself, so I can't tell you where is is. But somewhere, you can define subdomains of your domain, and that's what you're looking for.

Hopefully someone with more knowledge of CPanel-based setup will stop in here... Or try a site search.

Jim

10:44 pm on Sept 3, 2003 (gmt 0)

Preferred Member

10+ Year Member

joined:May 24, 2003
posts:381
votes: 0


There should be a very clearly label subdomain button. Couldn't tell you where it is, since I don't know which cPanel theme you're using.
6:45 am on Sept 4, 2003 (gmt 0)

New User

10+ Year Member

joined:Sept 1, 2003
posts:30
votes: 0


Yup I know how to add a subdomain.

I don't know how to add a *.mydomain.com (which means that any subdomain not defined goes to my website).

Actually, I've checked my DNS A records.

mydomain.com leads to IP Address X
*.mydomain.com ALSO leads to IP Address X

However, when I type *.mydomain.com (where * is some gibberish), it will lead to my host's welcome page (those kind of domain parking page).

Only when I define a subdomain, then will the subdomain lead to my webpage.

Doesn't make sense, does it, since mydomain.com leads to IP address X and so does *.mydomain.com

2:47 pm on Sept 4, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Sept 16, 2000
posts:122
votes: 0


For anything.mydomain.com to work *.mydomain.com must be in the zone file (DNS) and the Apache VirtualHost directive

However, when I type *.mydomain.com (where * is some gibberish), it will lead to my host's welcome page (those kind of domain parking page).

If there is no wildcard in the VirtualHost directive the subdomain does not exist so the first VirtualHost found in httpd.conf is served, probably your host's welcome page

The following line needs to be in your VirtualHost directive
ServerAlias *.mydomain.com

Then you will be able to use the following for sub-domains

# This should redirect without a directory named user
RewriteEngine on
RewriteCond %{HTTP_HOST} ^user\.example\.com [NC]
RewriteRule ^(.*)$ /blog.php?username=user [R=301]

# This example should serve pages from user2 directory
RewriteCond %{HTTP_HOST} ^user2\.example\.com [NC]
RewriteCond %{REQUEST_URI}!^/user2/ [NC]
RewriteRule ^(.*)$ /user2/$1