Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryTutorials and Answers

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

To WWW or Not to WWW (Or to Do Both)

Image for 'To WWW or Not to WWW (Or to Do Both)' article.

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

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC