homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

forwarding all subdomain to www ?

 4:41 pm on Oct 1, 2013 (gmt 0)

could I get some help on forwarding all subdomains within a domain to the www of my domain. Basically, all references to go to the www. of my domain. (no subdomains at all)

this means :

m.mydomain.com > www.mydomain.com

mydomain.com > www.mydomain.com

anysubdomain.mydomain.com > www.mydomain.com

basically yes, everything to www.mydomain.com

#1 Is this possible to do in DNS management in the CNAME field. ? If so, this would be my most preferred route.

#2 .htaccess preferred secondly.

#3 /vhosts/mydomain.com/conf preferred 3rdly.

Linux / CentOs system.

I appreciate the help....this is something I never needed done before...but so much initial generic traffic going to m. (mobile) versions of my site...even though I don't have any mobile versions. I dislike all subdomains in general...plus a lot of times they create duplicates of ur site home page, without u knowing.



 9:28 pm on Oct 1, 2013 (gmt 0)

Do you get enough requests for the (nonexistent) m. subdomain that you want to capture and redirect them, rather than simply let the DNS send out a "can't find it" error messsage? This should probably be answered first, before you get into the how-to.

A subdomain redirect can definitely be done in htaccess. It's one rule with one condition. In fact if you currently have subdomains, it may be a simple matter of eliminating any rules that pertain to the subdomains, leaving only the domain-name-canonicalization redirect.

But if it's your own server, you don't need to bother about htaccess. If you can't face the DNS issues, you can put the identical RewriteRule in your config file.

This is assuming you're on a setup that currently allows wild-card subdomains and there's no way to disable this feature. Otherwise it would make more sense to enable only those subdomains you actually use, or formerly used. From your post, it doesn't seem as if you have any.

to go to the www. of my domain. (no subdomains at all)

If you want to be hair-splittingly technical, www. is a subdomain. By convention it is treated as synonymous with the bare domain.


 11:21 pm on Oct 1, 2013 (gmt 0)

Is this possible to do in DNS management in the CNAME field. ?


Do you get enough requests for the (nonexistent) m. subdomain that you want to capture and redirect them, rather than simply let the DNS send out a "can't find it" error messsage?

there is no way for a webmaster to know what DNS requests are being made for various subdomains.


 1:53 am on Oct 2, 2013 (gmt 0)

Lucy, It's only like around 5 requests a day for the m. versions...and I have no content on any of the subdomains. (unless u consider www, in which I have all the content on)

yup, on the root access aspect. so i'm guessing conf is the route to go with.

I'd prefer playing around in the :


area instead of the server-wide



phranque, thx for confirming it being a no-go in the CNAME area. I thought there was a remote chance, but I guess not.

there is no way for a webmaster to know what DNS requests are being made for various subdomains.

that's another part of the reason why I wanna eliminate subdomains altogether. I didn't even know there were duplicate copies of my homepage on another subdomain within the same domain for like over a year....until noticed with MajesticSEO. There wasn't even a physical directory for that subdomain, it was just using / resolving to whatever was at the domain IP for the subdomain content. (my home page) So blahblah.mydomain.com was showing another copy of the home page. :(

awaiting the next step..

(any way we could switch my join date here to like 2012? ...waiting for some dork to be like : "dude, you joined like 8 years ago...and are still asking these kinds of questions?" ) :-/


 2:42 am on Oct 2, 2013 (gmt 0)

as implied by lucy24 you can either:

prevent the non-canonical hostnames from resolving which will also prevent urls on those hostnames from being indexed


accept and properly redirect requests for non-canonical hostnames


accept requests for non-canonical hostnames and return a 404 status code.


 7:25 am on Oct 2, 2013 (gmt 0)

In mod_rewrite terms:

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
This is the rule you've probably already got in some form.

same, only now it's preceded by a penultimate rule
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteCond %{HTTP_HOST} [a-z]+\.example\.com [NC]
RewriteRule .? - [G]

going with 410 over 404 primarily because it gets rid of google faster. (Bing doesn't seem to care.) Besides, it saves having to say [R=404] which looks goofy.

Further permutations if you've got multiple domains passing through the same config file.

involves changing DNS stuff. I have no idea how you do this in real life. On shared hosting you click a button and the fairies take care of it for you. At least they do if your host is also your registrar.


 6:06 pm on Oct 3, 2013 (gmt 0)

thx much Lucy.

I had #2 setup before posting here, but yea...it's conflicting with other rewrite rules that i'll have to dig deeper into. It's either conflicting with issues with Plesk, or something in my /conf/ file. (vhost.conf)

But I appreciate you confirming the correct method(s) to tinker with.


 8:02 pm on Oct 3, 2013 (gmt 0)

or something in my /conf/ file. (vhost.conf)

So you do have access to the config file. Then you don't need to have anything in htaccess-- and especially not mod_rewrite. Put it in the appropriate <directory> section in config instead.

In mod_rewrite, inner rules override outer rules. Outer rules don't kick in at all unless you're set RewriteOptions to inherit (never the default).

Someone else will have to explain how this works when "inner" is htaccess and "outer" is config of some type, because it isn't deducible from the docs :(


 7:51 pm on Oct 16, 2013 (gmt 0)

I found out whats causing the conflict.

the #2 rule you provided works fine (as it should for all) but only if I disable an important aspect of coding I use in vhost.conf.

in vhosts/example.com/conf/vhost.conf

I have the following code :

AddType application/x-httpd-php .php .html .htm
AddType application/x-httpd-php-source .phps
<Directory /var/www/vhosts/example.com/httpdocs>
Options ExecCGI
allow from all

Having this code together with the #2 example you provided... in .htaccess seems to cause apache to go nuts, and I just get an apache default message..that says apache is working on this domain.

but removing the above coding from vhost.conf , causes the #2 example to work perfectly fine. (non www going to www)

So...why do I need the above coding anyway ? It's used as an SEO enhancement to parse PHP pages on .html pages. I use a custom php program, which is why I need that.

Even if I bump the /httpdocs directory with the above coding into another directory...the redirect coding works fine as well. So it's something with having both codings working on /httpdocs thats causing confusion for apache rewrite.

any ideas on this aspect ? I know it's a little above grade for regular tech support...but any tips are appreciated.


 5:00 am on Oct 26, 2013 (gmt 0)

guessin nobody wants to give it a shot...?


what I have in /conf/vhost.conf in quotes above....

is what's preventing Lucy's #2 (coding example) from working.

so my domains still operate as follows :

example.com > example.com

www.example.com > www.example.com

maybe another work-around possible to get :

example.com > www.example.com ?

wondering too...how much of a penalty google see's this as 2 copies of the same home page. www and without www.


 8:48 am on Oct 26, 2013 (gmt 0)

Somewhere along the line your question shifted from "help me hammer out this Regular Expression" to "I need someone who speaks Apache" ;)

For me, looking at a segment like
<Directory /var/www/vhosts/example.com/httpdocs>
Options ExecCGI
allow from all

is like a monkey peering at the innards of a watch. "Allow from all"? Why do you need to allow from anyone? ... And that's why you need someone who speaks Apache.

Sometimes more people drift by on the weekend. Unless they're all in Las Vegas.

Is it the <Directory> envelope, the AddHandler lines-- or both in conjunction-- that makes Apache throw fits?


 2:17 pm on Oct 28, 2013 (gmt 0)

So you think eliminating that part is step 1 ?

my initial thought was that was accepting all visitor requests... to parse php on .html.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved