Force 'www' for all subdomain subsites - how can I do this so it applies to all new sites?

Hi WPMUDev,

I hope your day is going well!

I need to have all sites - both mapped domains and bare sub-domains - use 'www' and need some help figuring out how best to do this.

I have my primary setup at http://www.tivism.com (and yes, also *.www setup)

I know that I am able to use 'www' with Domain Mapping, so mapped domains are taken care of during the mapping step.

What I am looking for now is how to do the same for my bare sub-domain addresses.

I have the following code as a possible solution (from here), to be added into htaccess after “RewriteEngine On”:

# DOMAIN #1 - Redirect non-www urls to www
RewriteCond %{HTTP_HOST} ^domain1.com
RewriteRule ^(.*)$ http://www.domain1.com/$1 [R=301,L]

1) Is this code appropriate and alterable for use with subdomains on a network with Domain Mapping?
1a) something like:

# SUBDOMAIN #1 - Redirect non-www subdomain urls to www sundomain urls
RewriteCond %{HTTP_HOST} ^subdomain1.domain.com
RewriteRule ^(.*)$ http://www.subdomain1.domain.com/$1 [R=301,L]

?

I am only recently learning to read this stuff, I'm still a far way from being able. Your help is so awesome to have. :slight_smile:

2) I would vastly prefer a solution that did not require a manual edit of htaccess for every site. Got any suggestions?

3) Does a) the above code and/or b) your suggestion also work with Multi-Domains?

4) and lastly, any issues with ProSites or other WPMUDev plugins in doing this? (trying to really cover my bases here)

Thanks ahead of time for steering me in a good direction.

Regards,
Max

  • Ajay

    Hi @Max ,

    Hope you are doing well.

    Normally it related to alias host DNS resolver

    with .htaccess you can try this:

    RewriteCond %{HTTP_HOST} !^(www\.|$) [NC]
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

    Its take regex so you can try and test with above RewriteCond

    in httpd conf virtual host conf for your domains or sub domains.

    AT server / domain / cpanel / panel
    with domain panel can set WWW A record for main domain
    and CNAME for main , few panel auto config for main domain www

    Create a vhost.conf file in the /var/www/vhosts/system/example.com/conf/
    as ServerAlias http://www.sub.example.com

    Add an appropriate A record to the DNS zone of the domain
    http://www.sub.example.com. A 192.168.1.1

    Hope this help you.

    Regards,
    Ajay

  • wp.network

    Hey @Ajay and @Tyler Postle,

    My host basically recommended doing this in my database, as I described at length in my above linked thread wherein @aristath pointed out the intrinsic performance costs of my host's solution.

    @aristath seemed to think that your solution was preferable @Ajay, and it seems to me that it is less likely to cause issues with plugins (multi-db?) than the database route (not that I know what I'm talking about there, 'cause I don't).

    I will give it a try, and update with results.

    One difference from the above suggestion is that working with CloudFlare, all 'www' records point to unique CF CDN addresses.

    We'll see how this works out.

    Cheers,
    Max

  • Ajay

    Hi @Max,

    My host basically recommended doing this in my database

    Never ever required db based.

    All this things is resolved by DNS Zone , to work it proper way, and .htaccess support server based user config. Any other way is kind of redirecting url as script based, that is mostly deal with url query string part
    for working with controller,action, views etc.

    CDN:
    cdn mostly use to load balance the http request of static and media content. ex: images, js, css, video etc.

    Its preferable to get the best result from cdn use any other domain name even rather sub.domain.com

    you can use cdn.maindomain.com , but here you are prefixing www to your subdomains and may others subdomains name can confuse cdn.

    as you ever seen yahoo using yimg.com, google using gogle.com kind of another domain to serve cdn content. One another reason as same domain some time not to call proper way in programming url rendering and then cookie issue comes with cdn subdomain and this issue also not well for cache as some cache not accept cookie content.

    share your ftp , any one can try with htaccess code
    and for cdn better to use another domain for better result and clean separation. ask wpmu staff how to pm here.

    If you have any other domain name , then send me or i will help you book new domain.

    if you need more help, then give me two domains name, i will provide you one new cpanel account and another to new vps account. you can use both of them if that will serve your purpose and want to keep then will discuss, till then try, use as free from my side, that s i can do for you being a wpmu member.

    If you do not required badly, then not need to go for www to subdomain
    as lots of time confuse people to prefix www to subdomain.

    Regards,
    Ajay

  • wp.network

    @Ajay you have certainly helped me with your input. Thank you.

    You are operating a bit above my pay grade, as the saying goes.

    I believe I understand what you are saying about this being mostly a DNS thing. That was my general sense as well.

    As for using the 'www' as a cdn address, well, that is not my choice.

    I am using CloudFlare as a great free value DNS/CDN/Security with a low step into paid services on a per site basis as needed, with HTTPS capacities as well. My hosting budget is fairly low and will only grow with revenue.

    My host (SiteGround) like many others has integrated a CloudFlare UI extension into their cpanel. I am using this primarily as a way to ensure a tight fit between SiteGround, my network and CF. This requires using 'www' cname records to point to the CF CDN service.

    So it goes. I'd rather never see another www if I had my way.

    In any case, for now, this is my path. ;-P

    I sincerely appreciate your generous and considerate offer to provide more help, and even to do a trial setup. At the moment, I'm going to continue with the project at SiteGround, but again, thank you. Its not everyday one hears such an offer!

    I have just about got things set in a way that I think will work DNS-wise and am going to throw your htaccess lines into the mix... They should supply the missing component.

    I'll let you know how it goes :slight_smile:

    Kind Regards,
    Max

    ps. thanks also for the offer to help with domain names, though in that department I am perhaps overstocked and over budget... that is my main driver in developing this multisite. I have several hundred domains I want to develop, and I've been working for almost two years now to get to this point with WP. Once the network is up I can develop out my own sites very effectively, while offering my service to local small businesses. Thats the plan anyways. :slight_smile:

  • Tyler Postle

    Hey Max,

    Thanks for that link! Read the first bit, and it sounds really interesting - especially for someone who doesn't have much experience with Cloudflare, like myself :p bookmarked it for later.

    Just checking in here, been trying to get caught up on this thread:

    If it wasn't obvious

    Do you mean the setup or a specific part? I'm just not 100% sure which part this refers too. Or if you were more directing that towards @ajay - hope I'm not intruding on the conversation here :slight_smile:

    If you have time constraints then I would recommend always having that tech support addition. Will allow you to focus more on growing your business.

    Anyways, it's been a few days since your topic. So let us know if there's anything else you needed here or wanted to add.

    Cheers,
    Tyler

  • Dean

    wp.network I'm basically having the exact same issue you were in this thread (Cloudflare / WPMUDev / SiteGround). I am half way there but was really wondering did you ever manage to get this working without changing the URLs in the database?

    I would assume you would need to change both the URLs in the database and add a .htaccess rule. Otherwise when WordPress creates links in menus etc it will be the non-www version and whenever someone clicks a link it would need to be redirected to the WWW version...

    Did you ever get this to work? What did you do? I would really appreciate it if you could drop a short follow up message here. Everyone I ask via the chat supports all say different things. Although the overall remark is usually just don't use WWW on subdomains .

    Thanks,
    Dean

    • wp.network

      Hi Dean I'd be happy to help if I can... however, lets do it like this: if you haven't already, open a support ticket for your issue first, and then link to it in a reply here. Please try to describe:
      - what you're trying achieve overall, briefly
      - why is using 'www' on subdomains significant in your use case, again briefly
      - also, please include basic server info (eg. apache, php7.?, etc.)
      - how have you tried to achieve this
      -- what is working as expected
      -- what is not working as expected
      -- any ideas for moving forward/what does SG support say

      ...if you'll play along, then one way or another between WPMUDEV staff and community we'll get you sorted out :slight_smile:
      Cheers, Max

      [edit: well, just noticed that the comment is from Jan 27... I just saw the date before I think... anyways, offer holds if you still have a need, and anyone else finding this thread later should make their own ticket as outlined, etc :-]

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.