Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I have been trying to make a subdomain from a directory (made by .htaccess) to be used as the main page starting like http://domain.com/sub/2/3/4.html and turning into http://sub.domain.com/2/3/4.html.

The folder system goes like:

www\sub\2\3\

in www there is a index.html and in the sub folder there is a different index.html.

I currently have this code which I found on another site:

Options -Indexes +FollowSymLinks

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

RedirectMatch 301 ^/sub/(.*)$ http://sub.domain.com/$1

It is successful with changing the domain path to http://sub.domain.com/, but it is not successful in changing the page shown from the index in the www directory to the index in the sub directory.

It is also not successful in allowing to open other pages, i.e. not allowing sub.domain.com/2/3/4.html. If anyone can give any advice it would be much appreciated.

share|improve this question
add comment

migrated from stackoverflow.com Dec 16 '11 at 2:52

This question came from our site for professional and enthusiast programmers.

2 Answers

up vote 1 down vote accepted

I have found some fantastic code for the .htaccess that converts any folder in the main www directory into sub-domains e.g. www/somefolder turns into somefolder.domain.com you can also set it so that any directory inside a certain directory becomes a subdomain e.g. www/folder/somefolder turns into somefolder.domain.com

the code below is set to redirect directory's inside a specific directory

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]

simply change 'example.com' to your domain e.g. 'domain.com' make sure you don't remove any of the other symbols apart from dots . currently the directory's in the folder called 'subdomains' will change into a subdomain e.g. www/subdomains/blog will turn into 'blog.domain.com' to set it to just change the folders in the www directory to subdomains e.g. www/blog will turn into 'blog.domain.com'

if you want to automatically forward to a subdomain e.g. blog.domain.com you will need to put this at the end of the other code

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://blog.example.com/$1 [R=301,L]

hope this works for everyone it took me ages to find a solution to this horrible problem thanks Reblerebel

share|improve this answer
add comment

You need to configure a apache virtual host for sub.domain.com with www/sub as its DirectoryRoot.

share|improve this answer
    
how would you do that? –  reblerebel Dec 14 '11 at 21:42
    
    
so do i put that in the .htaccess file because i am unable to edit any other preferences –  reblerebel Dec 14 '11 at 21:51
    
No, this is not a .htaccess configuration. You need to edit apache's apache2.conf or httpd.conf. You'll have to ask your admin to do it then. –  jman Dec 14 '11 at 21:55
    
i am currently using free hosing which i can FTP into so would it go into the www directory? –  reblerebel Dec 14 '11 at 22:00
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.