URL Rewrite subdomain to folder


I need some help. I want sub-domains in the URL to load contents from defined folders. For example :

should show the contents of mysite/video. Moreover, if some one visits
it should load

Here I must mention that this should still show the re-written url - means should not just redirect.

I tried my self, but no success - This was my code:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^.mysite\.com$
RewriteRule ([^/]+)/?$ http://$1.mysite.com/ [NC,R=301,L]

Please Help !


There are many problems with what you’re trying to do and how you’re trying to do it.

First, subdomains are actually domains so they must be declared in order for the request to be delivered to the server.

Second, Apache has no way of knowing that socialvideos should resolve to video.

Third, {HTTP_HOST} is NOT case sensitive so you need to use the No Case flag.

Fourth, ^.mysite.com$ should NEVER match because the server should not ever receive {x}mysite.com (where {x} is any single character).

Fifth, trailing shashes normally cause problems with internal links. Fortunately, that should not be a problem here.

Sixth, where did video=1234 come from?

Finally, ([^/]+) will match file requests (the dot character has not been excluded, only the slash) so EVERYTHING (in the DocumentRoot) will get redirected.

Okay, to do what you’re after:

  1. Have the subdomain name match the folder name.

  2. The key/value pair must be requested.

  3. Use:

RewriteEngine on RewriteCond %{HTTP_HOST} ^(www\.)myside\.com$ [NC] RewriteRule ^(subdir1|subdir2|subdir3)/(.*)$ http://$1.mysite.com/$2 [L]

That will test for the www’d and “naked” version of your domain name then attempt to match the subdirectory names associated with the subdomains as well as the file requested. The %{QUERY_STRING} will not be affected.

You might benefit from reading the mod_rewrite tutorial at http://datakoncepts.com/seo as it contains explanations and sample code. It’s helped may members and should help you, too.



Thanks a lot DK for your detailed answer - this is really helpful and very professionally guided.

I regret that I did not post my reply earlier - was out of town.

Okay got these idea. Have another question if you can reply / help (others are also invited)

What is the possibility that:


will go ot:


But the url will stay subdomain.mysite.com

Hi Zman!

As above: “First, subdomains are actually domains so they must be declared in order for the request to be delivered to the server.”

This is critically important because browsers are created to ensure that website piracy does not occur (it can but that’s not what you’ve asked about). That means that, when you change the (sub)domain, a new request is generated and you CANNOT show the original request in the browser’s location box. IMHO, that is a very necessary and welcome feature.

On the other hand, IF your subdomain is co-located with the main domain, you could retain the subdomain.domain and serve process.php?what=subdomain (as long as the subdomain is registered with your DNS).