Getting Subdomains to Display With the Desired URL (http://subdomain.mysite.com)

Typically, when a subdomain is created for a website, the website owners want the visitor to see the URL as http://subdomain.mysite.com. However, you can usually access the subdomain also via http://mysite.com/subdomain.

But as stated above, http://subdomain.mysite.com is by far the preferred URL.

As you know, most web hosting companies allow multiple subdomains free of charge because a subdomain does not require a registered domain name of its own. Its webpage files merely reside in a directory or folder off the root. (That is why you can view the subdomain at http://mysite.com/subdomain.)

Also, most web hosting companies provide some kind of user interface, such as cPanel, by which a subdomain is requested. And cPanel usually offers the option to specify a redirect for the subdomain.

I don't use cPanel much since it's better to use a file transfer program to upload and manage website files. But when I would, I could never access the subdomain the desired way: http://subdomain.mysite.com. I tried different redirects and I tried no redirects, but nothing worked. And it never mattered until today.

If you are having this problem, here is the solution. Add the following 3 lines of code to the .htaccess file residing inside the subdomain's directory. The lines following the # are comments:


#redirect directory to subdomain
RewriteCond %{HTTP_HOST} !^subdomain\.mysite\.com
RewriteRule (.*) http://subdomain.mysite.com/$1 [L,R=301]


If you don't have an .htaccess file in the directory, you can copy and paste the following code into a Notepad file and save the file in ASCII mode with the name .htaccess - with no extension. Then upload the .htaccess file to the subdomain's root.


# to prevent people viewing my htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
# disable directory browsing
Options All -Indexes
# prevent listing contents of directorys
Options -Indexes
#redirect directory to subdomain
RewriteCond %{HTTP_HOST} !^subdomain\.mysite\.com
RewriteRule (.*) http://subdomain.mysite.com/$1 [L,R=301]


If you are not familiar with the .htaccess file, tutorials and sample files abound on the internet - such as this overview on the javascriptkit.com website. Every website should have a .htaccess file; but proceed carefully because a poorly coded .htaccess file can make a website inaccessible.





4 comments:

Anonymous said...

Thanks so much for the help. The instructions my Host gave were useless, and this worked.

Jamie Starbuck said...

I've searched many places until i found this gold piece that help my subdomain look right!

Thanks a lot

Lowell Thompson said...

Thank you so so so much for this blog post. I coulndt find the answer ANYWHERE but here! Thanks!

Mr. B. said...

I am so mad I could spit. I've spent so much time trying to figure out why I can't get to my own dam blog that my host made SO EASY for me to install. Thank heavens I found your post. It took me a while because I'm htaccess challenged, but you did good. THANK YOU !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Post a Comment