Forum Moderators: phranque

Message Too Old, No Replies

creating subdomains with rewrite in .htaccess

subdomain creation

         

suthaharan

4:02 pm on Dec 28, 2007 (gmt 0)

10+ Year Member



Hi,

I need help in setting up a folder inside the root directory as a subdomain.

Folder structure
www.mydomain.com
-- abcfolder
---- index.html
-- xyzfolder
---- index.html

When someone types abcfolder.mydomain.com it should go to www.mydomain.com/abcfolder/index.html but the url should still showup as abcfolder.mydomain.com/index.html.

Can someone help me do this?

Thanks in advance,

Suthaharan

suthaharan

7:13 pm on Dec 28, 2007 (gmt 0)

10+ Year Member



Hi Guys,

The following code (picked from the forum) successfully redirect users to index.html page inside folder (for eg. httpddocs/folderabc/index.html)


RewriteEngine on

# Skip rewrite if subdomain is www
RewriteCond %{HTTP_HOST}!^www\. [NC]
# Extract (required) subdomain to %1
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com(:80)?$
RewriteRule (.*) %1/index\.html

If I have physical subdomains inside my domain which is in the same level as httpdocs ...
/httpddocs
../abcfolder/index.html
../xyzfolder/index.html
/subdomains
../site1/index.html
../site2/index.html

My problem here is I wanted users to redirect to abcfolder/index.html when they type abcfolder.mydomain.com and to site1/index.html when they type in site1.mydomain.com.

Waiting to hear a reply while I am searching through the threads for an ideal solution.

Thanks,
Suthaharan

jdMorgan

9:38 pm on Dec 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code appears to meet your requirements as stated, although you may not have stated your requirements completely. For example, what should happen if "abc.example.com/foo.html" is requested, rather than the index page at "abc.example.com/"?

Did you completely flush your browser cache before testing this code?

Jim

g1smd

10:02 pm on Dec 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The word "redirect" was used several times, when in fact it is probably a rewrite that is required.

suthaharan

11:17 pm on Dec 29, 2007 (gmt 0)

10+ Year Member



Hi Jim,

If I have physical subdomains inside my domain which is in the same level as httpdocs ...

/httpddocs
../abcfolder/index.html (subfolder: abcfolder)
../abcfolder/foo.html (subfolder: abcfolder)
../abcfolder/foo2.html (subfolder: abcfolder)
../xyzfolder/index.html (subfolder: xyzfolder)
/subdomains
../site1/index.html (subdomain: site1)
../site1/foo.html (subdomain: site1)
../site2/index.html (subdomain: site2)

For subfolders:
I wanted the rewrite to work in such a fashion that I can display the contents of all the html/php files inside that subfolder. With the above code, I can only display index.html file. I wanted all the files inside subfolders also to get displayed.

Is there a way to check in .htaccess to see if the subdomain typed in the URL is actually a physical subdomain folder created under /subdomains directory and then to check if it is a subfolder created in /httpddocs?

Thanks in advance.
Suthaharan

jdMorgan

1:47 am on Dec 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This demonstrates the use of a back-reference to 'copy' the requested resource name, and to create file-exists and directory-exists checks. You may need to modify the checked filepath to suit your needs.

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

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

suthaharan

8:38 pm on Jan 2, 2008 (gmt 0)

10+ Year Member



Hi,

Thanks for the reply. I am starting on with the cited links.

But the above code doesn't seem to work for me completely.

Now, if someone types http://abcfolder.example.com it is taking to http://abcolder.example.com/index.php page

If someone types the URL http://site1.example.com, it is picking up the index file from http://www.example.com/index.php and is not taking to the site1 subdomain as expected.

'/subdomains' is a folder in the same level of access as the folder '/httpddocs'

Thanks,
Sutha

[edited by: jdMorgan at 12:42 am (utc) on Mar. 10, 2008]
[edit reason] De-linked [/edit]

jdMorgan

9:23 pm on Jan 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You may need to modify the checked filepath to suit your needs.

Jim

TwoLittleWitches

10:37 pm on Mar 9, 2008 (gmt 0)

10+ Year Member



Worked perfectly... Thank you Jim.

One extra question, is there any way to work with case-sensitive subdomains/subfolders?
For example,

http://subdomain.example.com >> http://www.example.com/subdomain
or
http://SubDomain.example.com >> http://www.example.com/SubDomain

Thanks in advance,
Sam

[edited by: jdMorgan at 12:41 am (utc) on Mar. 10, 2008]
[edit reason] example.com [/edit]

jdMorgan

12:39 am on Mar 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Unfortunately, no. This is because most clients will force the subdomain to lowercase before sending it to the server. Otherwise, the code would indeed preserve the case of the requested subdomain by default; It's actually quite painful (in .htaccess) to do case-conversions, so that client-lowercasing may be a good thing.

Jim

tkstock

1:20 am on Mar 10, 2008 (gmt 0)

10+ Year Member



Jim,

I've tried all your suggestions above to my situation, and I can't get it to work.

Let me give you some information:

1. My host is HOSTGATOR - I don't have access to http.conf. Everything I must do through .htaccess.

2. I have almost no knowledge of .htaccess.

3. I've implemented the above code, but it fails when I attempt to go to any of the pages of the subdomain securely. It gives me a certificate error (which I expected), but it shows the content of the main domain instead of the subdomain.

I've got a site which I'm trying to implement a test subdomain with all the functionality of the main domain (secure and all).

Thanks for your help!

Tom