Forum Topic: Editing .htaccess for subdomains

Forum: .htaccess Forum : Redirecting • Posted by George Theodore • Updated:

I am currently using the following code in my .htaccess file to require the www in all requests. I also use a re-direct to remove the index.html to the directory.

RewriteEngine On
RewriteBase /

### re-direct IP address to www
### re-direct non-www to www
RewriteCond %{http_host} !^www.math-aids.com$ [nc]
RewriteRule ^(.*)$ http://www.math-aids.com/$1 [r=301,nc,L]

### re-direct index.html to root / ###
RewriteEngine on
RewriteCond %{THE_REQUEST} ^.*\/index\.html?\ HTTP/
RewriteRule ^(.*)index\.html?$ "/$1" [R=301,L]

My question is that I want to create a subdomain called “terms”. I create the subdomain in my cPanel interface, but when I type in terms.math-aids.com it changes to www.math-aids.com/terms

I want the request to go to http://terms.math-aids.com.

I realize that www is also a subdomain so what do I need to do to keep the www that is currently working and add different subdomains like “terms”, “videos”, “lessons” and have them resolve to terms.math-aids.com, videos.math-aids.com, lessons.math-aids.com ?

Thanks for your help.

4 Replies to “Editing .htaccess for subdomains”

Posted by Jeff Starr •

Hi George,

Normally it’s possible to redirect to subdomains, but it sounds like cPanel is interfering with how subdomain URLs are configured on the server.. Before using any htaccess, I would first ask your host why that is the case and if there is a setting or code or something that will restore normal functionality.

Or, another possibility is that the www redirect is causing the issue. Such that this:

RewriteRule ^(.*)$ http://www.math-aids.com/$1 [r=301,nc,L]

..is routing other subdomain requests as well. I’m not sure what you’ve heard, but the “www” prefix is not required for proper URLs. I would maybe try removing the prefix and then trying the subdomain redirects with a clean slate.

I hope something there helps, good luck!

Posted by George Theodore •

Jeff,

Thanks for you response. I guess I did not make my question clear enough. Let me try again.

I have 5 years of SEO and backlinks to my site. I started with the www.math-aids.com and I will NOT change that. I don’t want to risk losing the 500+ number one SERP positions for my keywords and my site.

I am currently running the htaccess code that I listed in the previous posting. I do know that if I don’t run any htaccess file that the subdomains will resolve the way I want them to like terms.math-aids.com.

I want to modify the htaccess file that I have listed previously to keep the www for all requests that don’t have a subdomain request, and for the requests that do have a subdomain I want them to resolve with the subdomain in the beginning of the URL. So here are the example to show what I am trying to get done.

Request math-aids.com → goes to www.math-aids.com
Request www.math-aids.com → okay nothing happens
Request terms.math-aids.com → goes to terms.math-aids.com

Thanks for your help, You are a great resource.

Posted by Jeff Starr •

Have you tried placing the www code in the htaccess file located in the root of the subdomains? I don’t think they will work when included in the htaccess file located in the root of the primary domain.

Edit to add that normal www-canonical htaccess should work fine if located in the correct location. Apologies if this is not the case.

I hope this helps!

Posted by Chris McCoy •

are you trying to create sub domains on the fly with htaccess without creating an actual apache entry for it? like map a sub directory on your domain to a sub domain with keeping the actual url in the browser on the sub domain?