Redirecting a subdomain to script with .htaccess

Tuesday, July 14, 2009 23:50
Posted in category .htaccess, PHP, Tutorial

I recently had to work with .htaccess to redirect user to their homepage based on subdomain. The requirement was simple and is seen here and there and ofcourse everywhere now a days in different social networks. The requirement was redirecting http://username.domain.tld to http://domain.tld/user.php?u=username. This is just an example (Not similar to url I used). So how do we accomplish this with .htaccess. With .htaccess it is easy to redirect a subdomain to a script. See the .htaccess code below and I will explain how to do it.

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ([^\.]+).domain.(.*)
RewriteCond %{HTTP_HOST} !^exclude.domain.(.*)
RewriteCond %{HTTP_HOST} !^www.domain.(.*)
RewriteRule ^(.*)$ user.php?u=%1 [L,NC]

(Use [R=301,NC] if you want to redirect the page instead of rewriting)
Whenever you use mod_rewrite (the part of apache that does the magic), you need to include these 2 lines before any ReWrite rules.
Options +FollowSymLinks
RewriteEngine On

+FollowSymLinks must be enabled for any rules to work (Security requirements)

The third line RewriteCond %{HTTP_HOST} ([^\.]+).domain.(.*) matches the host name (remember %{HTTP_HOST} is an environment variable.) and finds the subdomain (excluding any dot (.) in middle). The fourth and fifth line excludes the domain listed. This is necessary so that you don’t confuse the script to use unwanted subdomain. for e.g. in category.domain.tld the fourth line will exclude this domain. If we don’t exclude this domain it will rewrite to script with category as username.  (pretty neat ;) ). Also add as many subdomains you wish to use that are not users. The fifth line does the same. It excludes www.domain.tld to be redirected.

The sixth line does the magic. it rewrites to correct script with subdomain as parameter. (%1 stores the subdomain).

That is all. It worked for me. let me know if it did for you! Happy .htaccess(ing)!

VN:F [1.8.8_1072]
Rating: 9.3/10 (4 votes cast)
VN:F [1.8.8_1072]
Rating: 0 (from 0 votes)
Redirecting a subdomain to script with .htaccess9.3104
http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/digg_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/reddit_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/stumbleupon_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/delicious_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/blogmarks_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/google_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/myspace_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/facebook_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/yahoobuzz_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/twitter_32.png
You can leave a response, or trackback from your own site.

One Response to “Redirecting a subdomain to script with .htaccess”

  1. Nits says:

    March 16th, 2010 at 6:56 am

    i have set up a codeigniter framework, applied a code similar you have mentioned above in our htaccess file, but still it seems not working at my server, it redirects me to unknown path.

    Does this relate to my hosting panel controllers, some DNA settings? Please provide me a correct solution….

    UN:F [1.8.8_1072]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.8_1072]
    Rating: 0 (from 0 votes)

Leave a Reply