Forum Moderators: phranque
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
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
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
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