To WWW or Not to WWW (Or to Do Both)
If your domain can be accessed both with and without the "www." subdomain, you may run into strange cookie issues or find that some things, like forms or database accesses, intermittently fail. (The "www." in front of a domain name indicates a subdomain just like "books." or "anything.else." indicate subdomains when occurring in front of a domain name.)
There are ways to circumvent unwanted issues caused by incorrect subdomain match.
A common method is to force browsers to use or not to use the "www." when it requests a page or other resource. It's an .htaccess method described at various places on the Internet.
Perhaps the .htaccess method isn't an option for you. Maybe you need both www. and non-www. available. Or you don't have access to the .htaccess file; or prefer not to change it.
There are other methods to make cookies work with both www. and non-www. domains and also to keep the subdomain consistent with the browser's address bar throughout the entire page.
The other methods aren't as straightforward as the .htaccess method, but should work in most cases.
Making Cookies Visible at Both WWW. and non-WWW. Domains
When setting a cookie, specify the domain name with a leading dot/period character. Do not specify a subdomain.
Examples:
Domain in browser's address bar |
Domain for cookie |
---|---|
example.com | .example.com |
www.example.com | .example.com |
books.example.com | .example.com |
For setting cookies with PHP, see the Setting and Viewing Cookies with PHP article. And for setting cookies with JavaScript, see the Setting and Reading Cookies with JavaScript article.
Keeping a Consistent Subdomain Within the Page
It isn't often that one runs into this, but it does happen. Software may protest when it's accessed at a different subdomain than the subdomain of the URL in the user's browser. It may refuse to access a database or accomplish other functions without a consistent protocol. (With Ajax, consistency is required.)
If you've experienced that — or just wish to keep subdomain consistency within a page, perhaps for SEO or aesthetic reasons or to satisfy a personal sense of elegance — there are two ways to keep a consistent subdomain within a web page.
Method 1 — Relative URLs
Remove the protocol and domain name parts of the URLs that link to (or request resources from) other locations within the same website. Examples:
Absolute URL | Relative URL |
---|---|
http://example.com/page.php | /page.php |
http://example.com/images/image.jpg | /images/image.jpg |
https://example.com/domain.css | /domain.css |
When a relative URL is specified within the page, the browser assumes the same protocol and domain as the URL in the browser's address bar.
Method 2 — Absolute URLs
When absolute URLs are required, method 1 won't be an acceptable solution.
Instead, this PHP method may work.
As implied, the PHP method does require web pages to be PHP pages so the PHP code can work. But that doesn't limit what can be linked to. Pages and resources being linked may be .html, .php, .jpg, .css, .js, whatever extension the file has.
To implement, replace the domain portion of the URL with this:
<?php echo($_SERVER['HTTP_HOST']) ?>
Examples:
A.
Current URL:
http://www.example.com/page.php
Modified URL:
http://<?php echo($_SERVER['HTTP_HOST']) ?>/page.php
B.
Current URL:
http://example.com/images/image.jpg
Modified URL:
http://<?php echo($_SERVER['HTTP_HOST']) ?>/images/image.jpg
C.
Current URL:
https://example.com/domain.css
Modified URL:
https://<?php echo($_SERVER['HTTP_HOST']) ?>/domain.css
The server replaces the PHP code with the domain used by the browser to request the page.
Bonus Tip: Keeping SSL/non-SSL URLs Consistent
If you allow both SSL and non-SSL (https:// and http://) access to your website, URLs to images and other files imported into the page should match the URL in the browser. If non-SSL, https://... URLs work fine. But if SSL, then non-SSL (http://...) URLs will break the secure lock icon of the browser being used.
An easy solution is to omit the leading "http:" or "https:" protocol from the URLs. Examples:
From: https://example.com To: //example.com
From: http://example.com To: //example.com
The browser will assume the https: or http: according to the protocol of the URL in the browser's address bar.
As described in this article, there are a few ways to accomplish subdomain consistency. One of them should work for you.
(This article first appeared in Possibilities ezine.)
Will Bontrager