Web Hosting Talk


Go Back   Web Hosting Talk : Web Hosting Main Forums : Hosting Security and Technology : Hosting Security and Technology Tutorials : Mikey's Instant Subdomains
Reply

Hosting Security and Technology Tutorials Tutorials related to server security or the like.

 
Thread Tools Search this Thread Display Modes
  #1  
Old 05-25-2005, 04:40 AM
ub3r ub3r is offline
View Beta Profile
Disabled
 
Join Date: Dec 2002
Location: chica go go
Posts: 11,856
Mikey's Instant Subdomains

This tutorial will guide you on the steps needed to make subdomain creation as easy as creating a single directory.

Step One
Add a CNAME record for your domain for *.yoursite.com to your actual domain.

*.yoursite.com. 14400 IN CNAME yoursite.com.

make sure to include appropriate periods in those addresses.

Step Two
Open up your httpd.conf and add a host into your ServerAlias directive:

ServerAlias *.yoursite.com yoursite.com

Step Three

navigate to your site's public_html directory, and add this code to your .htaccess, if the .htaccess file doesn't exist, create one by typing "vi .htaccess", you can learn more about using vi at This Thread

RewriteCond %{HTTP_HOST} ^[^.]+\.yoursite\.com$
RewriteRule ^(.+) %{HTTP_HOST}$1
RewriteRule ^([^.]+)\.yoursite\.com(.*) /home/yoursite/public_html/$1


Now, you can simply create a directory inside your public_html directory, and the subdomain will be instantly available.


Last edited by SoftWareRevue; 05-25-2005 at 03:49 PM.
Reply With Quote
Sponsored Links
  #2  
Old 05-25-2005, 04:50 AM
ryno267 ryno267 is offline
View Beta Profile
Junior Guru Wannabe
 
Join Date: Mar 2004
Location: Arizona
Posts: 91
thanks mikey - that could come in handy in the future. Glad you figured that thing out - I'm so not the cgi/apache coder....

__________________
rYnoweb.com

Reply With Quote
  #3  
Old 05-25-2005, 09:38 AM
error404 error404 is offline
View Beta Profile
Web Hosting Master
 
Join Date: Dec 2004
Location: Canada
Posts: 1,076
Don't forget mod_vhost_alias .

Good stuff.

__________________
>> Keenan Tims
█▓▒░ I am currently looking for full-time work in the Vancouver area. Resume ░▒▓█
http://www.gotroot.ca/ | ktims@gotroot.ca | skype: keenan.tims

Reply With Quote
Sponsored Links
  #4  
Old 09-11-2005, 09:46 PM
mouseattack mouseattack is offline
View Beta Profile
Web Hosting Master
 
Join Date: Dec 2004
Location: Behind You.
Posts: 664
apologizes in advance for thread digging, but this is the only place I could find this code.

Ok so I did everything, but I guess there is something wrong in my .htaccess file, because no matter what I put in there, I see the MAIN www.mydomain.com website.


Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.mydomain\.org$
RewriteRule ^(.+) %{HTTP_HOST}$1
RewriteRule ^([^.]+)\.mydomain\.org(.*) /home/mydomain/public_html/$1


What did I get wrong? I'd really like to get this up and running, I plan to have

ANYSUBDOMAIN.mydomain.com
- read from
mydomain.com/~ANYSUBDOMAIN

Any help I can get is appreciated? I added the appropriate things in the /var/named/mydomain.com.db and in the httpd.conf file.

What can I do to get this mod rewrite running?

Thanks.

Reply With Quote
  #5  
Old 09-11-2005, 10:11 PM
Scott.Mc Scott.Mc is offline
View Beta Profile
Web Hosting Master
 
Join Date: Jan 2005
Location: Scotland, UK
Posts: 2,041
Do you have AllowOverRide set to none in httpd.conf? if so set it to all.

__________________
Server Management - AdminGeekZ.com
Infrastructure Management. High availability cluster management.
Contact Us Today United Kingdom(Toll Free): 0800 8620073 // United States: 585 563 1729 // Australia: 02 9037 2448 // International: +44.1412800134

Reply With Quote
  #6  
Old 09-11-2005, 11:19 PM
mouseattack mouseattack is offline
View Beta Profile
Web Hosting Master
 
Join Date: Dec 2004
Location: Behind You.
Posts: 664
<Directory />
Options All
AllowOverride All
</Directory>

That is what is set. What other bright ideas?

Reply With Quote
  #7  
Old 09-11-2005, 11:26 PM
mouseattack mouseattack is offline
View Beta Profile
Web Hosting Master
 
Join Date: Dec 2004
Location: Behind You.
Posts: 664
can someone replace mydomain and org with their domain and tld and test it and let me know if it even works or not?

I keep getting either 404 or 500 errors when i'm messing around with it.

I KNOW wildcarded domains is on for a fact, so the problem is not with that, because

ANYTHING.MYDOMAIN.ORG goes to www.mydomain.org

Reply With Quote
  #8  
Old 09-12-2005, 02:38 AM
mouseattack mouseattack is offline
View Beta Profile
Web Hosting Master
 
Join Date: Dec 2004
Location: Behind You.
Posts: 664
RewriteEngine on
RewriteBase /

# rewrite <subdomain>.example.com/<path> to example.com/<subdomain>/<path>
#
# Skip rewrite if no hostname or if subdomain is www
rewriteCond %{HTTP_HOST} .
rewriteCond %{HTTP_HOST}!^www\. [NC]
# Extract (required) subdomain (%1), and first path element (%3), discard port number if present (%2)
rewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+)\.zaxy\.org(:80)?<>/([^/]*) [NC]
# rewrite only when subdomain not equal to first path element (prevents mod_rewrite recursion)
rewriteCond %1<>%3!^(.*)<>\1$ [NC]
# rewrite to /subdomain/path
rewriteRule ^(.*) /%1/$1 [L]

I tried that code above, and I get 500 error.

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com [NC]
RewriteRule ^(.*) http://www.yourdomain.com/$1 [L,R=301]

I tried that code, and I believe this one worked, but it redirected the browser to the folder location, and I don't want that. I want anysubdomain.mydomain.com to READ from mydomain.com/subdomain/ , not redirect. I could do that with a simple normal 301. I tried replacing the

RewriteRule ^(.*) http://www.yourdomain.com/$1 [L,R=301]
with
RewriteRule ^(.*) /home/mydomain/public_html/$1/ but it didn't do any good. Still 500 error.

Reply With Quote
  #9  
Old 09-17-2005, 01:26 AM
extras extras is offline
View Beta Profile
Web Hosting Master
 
Join Date: Aug 2005
Location: Canada
Posts: 838
The backref in REGEX "\1" doesn't work in recent version of Apache.
rewriteCond %1<>%3!^(.*)<>\1$ [NC]

It seems this trick worked on older Apache, but not anymore, AFAIK.

Also, this code is useless.
rewriteCond %{HTTP_HOST} .

You can't do what you want in the .htaccess because of infinit looping.
You should put similar code in httpd.conf.

Other than per-dir context (.htaccess and <Directory>),
there is no problem of unwanted looping.
([L] works as expected.)

Maybe I'll write a patch to solve this, one day.

Reply With Quote
  #10  
Old 09-17-2005, 03:00 AM
extras extras is offline
View Beta Profile
Web Hosting Master
 
Join Date: Aug 2005
Location: Canada
Posts: 838
I found another way to stop infinite loop (not ideal, though...).

Code can be as simple as this:
RewriteEngine on
RewriteBase /
rewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com [NC]
rewriteRule ^(.*)$ %2/$1 [L]

But in each one of subdirectory for the subdomain,
there must be a .htaccess file that contains;
RewriteEngine Off
or even;
RewriteEngine On

These have teh effect of stopping the processing.
If the .htaccess is deleted or the code is removed,
it will create 500 error with max redirect (Apache1.3.28 and above)
or Apache going crazy (below 1.3.28).

Having said that, better method is using mod_vhost_alias,
or doing similar redirect in other than per-dir context (usually in virtualhost).
In case it msut be done in .htaccess, using a prefix to the directory names will solve the problem.
(The prefix can be anything that doesn't occur in other directories or files, like "S_", "_", "USER-","sub/" and so on)

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com [NC]
RewriteRule !^/*prefix_ prefix_%2%{REQUEST_URI} [L]

Reply With Quote
  #11  
Old 01-17-2006, 04:26 PM
elprogramero elprogramero is offline
View Beta Profile
New Member
 
Join Date: Jan 2006
Posts: 2
Hello,
I'm sorry for diging this thread out, but is there a way to do this in Apache 1.3.3. ?

I'm having trouble with this code:

Code:
Step One
Add a CNAME record for your domain for *.yoursite.com to your actual domain.

*.yoursite.com.       14400   IN      CNAME   yoursite.com.

make sure to include appropriate periods in those addresses.

Step Two
Open up your httpd.conf and add a host into your ServerAlias directive:

ServerAlias *.yoursite.com yoursite.com

Step Three

navigate to your site's public_html directory, and add this code to your .htaccess, if the .htaccess file doesn't exist, create one by typing "vi .htaccess", you can learn more about usin....

RewriteCond %{HTTP_HOST} ^[^.]+\.yoursite\.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 
RewriteRule ^([^.]+)\.yoursite\.com(.*) /home/yoursite/public_html/$1
Can anyone PLEASE help me...?

I have read the whole thread, but I can't find solution, and this is ONLY place on the web where I was able to find it.

Thank you.

Reply With Quote
  #12  
Old 01-17-2006, 05:01 PM
elprogramero elprogramero is offline
View Beta Profile
New Member
 
Join Date: Jan 2006
Posts: 2
Hello... It's me again.. .Sorry for not understanding the tread, but I have solved my problem...

Sorry again....

Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement: