My site is finally accessible with “www” URLs such as http://www.walterstovall.online to reach my home page. It might seem weird that I would just now get around to that but I go where I’m drawn. I consider it a milestone in my site-build that I even care 🙂 In reality this is a little more than forgiving assumptions in the mind of the person trying to access my site. Apparently at least this also affects the ability of at some bots to crawl the site too.
To be clear, for most sites this is probably a non-issue. When you get a domain name the “www” variant comes along for free and maps to the same IP address. So the internet already knows that “www.walterstovall.online” means the same thing as “walterstovall.online”. Either way the accessing client gets the same IP address and that says everything about which computer receives the request. End of story? Not really…
Even with an ordinary site there still might be security-certificate issues with “www” versus not? I’m not sure but maybe you have to specify a SAN even in the normal case, so you can load HTTPS/SSL resources? But with my solution below I don’t even face those issues so I’m not sure about that.
The blocker to www URLs for me centered around my use of virtual hosts in order to have multiple domains that map to this same server. When a HTTP request comes to my server, in order to know how to handle it the server has to look at which logical host the client is trying to access. When it sees that it’s “walterstovall.online”, the server knows to access the associated virtual host computer (but this is not really a virtual computer it’s just a mapping to the directory system/apache instance/php). The mapping says everything about which web site you land at.
When the request is for “www.walterstovall.online” the server does not see that as referencing a virtual host. For all the server knows “www” is a subdomain you’re trying to get to. In my case that would land the user on a empty placeholder I have now for the server’s home page.
Like a lot of things the solution to this problem is trivial – IF you know how to launch yourself into the right context and execute those simple steps. I’ve been pondering where exactly that is…DNS records? No. Luckily for me sipping my coffee this morning and searching with google a little bit is all it took to land on exactly the right thing.
My google search took me to Simone ‘s Blog where he lays out a beautifully concise solution that fits my situation exactly. As explained there this will redirect ANY “www” URL to the non-“www” variant. This also throws in the important redirection of HTTP to HTTPS (which I already had covered but not in this important additional context).
This works perfectly for me because my conical name is “walterstovall.online”. Anything else that should land you in the same place is an alias for the real site.
Following the above instructions I created a .htaccess file at my /web folder with this content:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www. [NC]
RewriteCond %{HTTP_HOST} ^(?:www.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
For me this is the /web of the volume not the logical /web of the virtual host. So this mapping applies to all domains at my site not just walterstovall.online.
Security being important as always I’m also setting permissions on the file per best practices…
chmod 644 .htaccess
Before this my site was accessible as https://walterstovall.online and http://walterstovall.online. With this little change a user can now get to the site with https://www.walterstovall.online or http://www.walterstovall.online.
Freeing up brain cells for other stuff! 🙂
Glad you got this all figured out!
Thanks for caring 🙂 Another detail squashed… I didn’t realize I’d care but now I can go to my site by just typing “walterstovall.online” in the address bar with no https://. That rolls off my fingers smoother than finding and clicking on a link.