Forum Moderators: phranque

Message Too Old, No Replies

Create Virtual Subdomain for particular folder in .htaccess

Virtual Subdomain in .htaccess

         

shaileshgajjar

2:15 pm on Apr 30, 2008 (gmt 0)

10+ Year Member



Hello Everybody,

Please help me

I need to create the subdomains for particular folders using .htaccess

public_html/subdomains/world
public_html/subdomains/green
.....
world, green , etc are the folders i need to create the sub domains for them.

I want to create the sub domains like [world.yourdomain.com...] which link to this physical path ( public_html/subdomains/world )

I used this script with my Dedicated Server,

Options FollowSymLinks
RewriteEngine On
RewriteBase /

# Skip rewrite if subdomain is www
RewriteCond %{HTTP_HOST} !^www\. [NC]
# Extract (required) subdomain to %1
RewriteCond %{HTTP_HOST} ^([^.]+)\.yourdomain\.com(:80)?$
# Skip rewrite if requested URL does not resolve to existing file or subdirectory in /<subdomain> path
RewriteCond %/home/abc/public_html/subdomains/%1/$1 -f [OR]
RewriteCond %/home/abc/public_html/subdomains/%1/$1 -d
RewriteRule (.*) /subdomains/%1/$1 [L]

It is not working.

Please help me to solve this issue.

Thank you,

Every body

jdMorgan

3:36 am on May 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code does not work, because it was designed to map subdomains to subdirectories, and your description states that you want to map a subdomain of the www subdomain to a subdirectory.

Also, the RewriteCond %/home/abc/public_html... lines above are is incorrect. They should probably read
RewriteCond %{DOCUMENT_ROOT}...
Or if you really wish to hard-code the path, then remove the leading "%" symbol and use
RewriteCond /home/abc/public_html...

The code above (if corrected) should work with http://world.example.com and http://green.example.com, but will not work with http://www.world.example.com or http://www.green.example.com.

Jim

shaileshgajjar

5:17 am on May 1, 2008 (gmt 0)

10+ Year Member



Thank you for your reply.

My File Structure:

/public_html/subdomains/green/ ( I have created this folder and put one index.html file also)

My Corrected code:

Options FollowSymLinks
RewriteEngine On
RewriteBase /

# Skip rewrite if subdomain is www
RewriteCond %{HTTP_HOST} !^www\. [NC]
# Extract (required) subdomain to %1
RewriteCond %{HTTP_HOST} ^([^.]+)\.yourdomain\.com(:80)?$
# Skip rewrite if requested URL does not resolve to existing file or subdirectory in /<subdomain> path
RewriteCond %{DOCUMENT_ROOT}/subdomains/%1/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/subdomains/%1/$1 -d
RewriteRule (.*) /subdomains/%1/$1 [L]

Checking with this subdomain:
[green.yourdomain.com...]

It is still not working. Please guide me what is wrong ?

Thank you,
Shailesh

jdMorgan

5:14 pm on May 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We have many requests here, and very few contributors to answer them. Also, it is up to you to study the documentation [httpd.apache.org], and to understand, test, and debug your own code. We can help, but the work is up to you. Please see our Forum Charter [webmasterworld.com] for more information.

That said, I'd suggest:


Options +FollowSymLinks -Multiviews
RewriteEngine on
RewriteBase /
#
# Canonicalize the hostname
RewriteCond www.%{HTTP_HOST} ^(www)\.(example\.com) [OR]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.(example\.com) [OR]
RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.(example\.com) [OR]
RewriteCond %{HTTP_HOST} ^([^.]+)\.(example\.com). [OR]
RewriteCond %{HTTP_HOST} ^([^.]+)\.(example\.com):[0-9]+
RewriteRule (.*) http://%1.%2/$1 [R=301,L]
#
# If subdomain is NOT www
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
# Extract (required) subdomain to %1
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
# Rewrite if requested URL resolves to existing file or subdirectory in /subdomains/<subdomain>/ path
RewriteCond %{DOCUMENT_ROOT}/subdomains/%1/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/subdomains/%1/$1 -d
RewriteRule (.*) /subdomains/%1/$1 [L]

Two potential problem areas: First, this code must be located in the .htaccess file of a directory which will be accessed, no matter which subdomain is requested. From your description, that would be /home/abc/public_html/.htaccess

Second, the %{DOCUMENT_ROOT} variable must also contain the exact path /home/abc/public_html in order for the two 'file exists' RewriteConds to work properly. If in doubt, you may comment-out the two file-exists RewriteConds for testing. If the rule then works, then the %{DOCUMENT_ROOT} path is probably incorrect. Either that, or the folder /home/abc/public_html/subdomains/<subdomain> is probably missing.

Jim