Domain name basics and redirects

D

Learn more about the basics of domain names and especially why it’s important to have redirects for www/non-www and http/https configured correctly with your DNS and server-side scripts.

What is a primary domain name?

A website’s primary domain name is the domain name that you give to visitors. A domain is an online address that you register with a domain registrar, and the domain is the name chosen to represent your site. Some refer to your primary domain name as the REAL domain name, root domain, hostname, host, default URL, or apex. For more details about the nuances of these terms, you might enjoy, “What happens when you type a URL in the browser and press enter.”

An example of a domain name is google.com or netflix.com. A domain name translates to an IP address. Domain names are quick, easy-to-remember online addresses. To access websites, users type in memorable domain names as an alternative to typing in numbered addresses. For instance, a user types into their browser netflix.com rather than a list of numbers like an IP address of 123.123.2.1.

What is a subdomain?

A subdomain is a subset of a domain. A subdomain is the letters/numbers/symbols preceding your domain name. A period separates the subdomain term from the domain name by a period. A subdomain is considered a different website from the primary domain. The subdomain is associated but is a stand-alone site. Businesses commonly use subdomains for mail, staging, or testing. A subdomain might look like this:

mail.example.com
staging.example.com
testing.example.com
www.example.com

What is www?

The letters www in a URL stand for world wide web and are considered a subdomain of a domain. For instance, www is the subdomain of example.com in the URL www.example.com.

In most cases, the intention of the www subdomain is not to be a stand-alone site. Instead, the URL with a www is intended to be the same site as the URL without the www. It is meant to show the same content and redirect to the same URL.

A note about usability: When manually typing a URL into a browser, www is a prefix many users forget to type or intentionally skip inclusion. Graphic designers also prefer to omit ‘www’ from URLs in marketing materials. Therefore for optimal usability, a website needs to display the intended primary content, whether a www is included in the URL or not. Misconfiguring this redirect setup will result in errors for users.

What is a naked URL?

A naked URL is a URL that omits the letters/subdomain www before the domain name. Any domain name without the ‘www’ prefix can be referred to as a naked or bare domain.

What is a canonical domain?

Your canonical domain is the domain name you’ve decided is your official website. The canonical domain is also referred to as the real URL, preferred domain, or the domain name that is most authoritative, especially in the eyes of search engines for SEO.

It is important to note that search engines consider www.example.com and example.com to be two different sites, unless told otherwise. The same goes for the use of HTTP and HTTPS. Unless told, search engines do not know which of this duplicate content to display. (Read more about duplicate content on moz.com and on ahref’s blog about canonical tags)

Before 2019, webmasters could alert Google to their preferred domain name from within Google Webmaster Tools (now Google Search Console). After 2019, Google determines your preferred domain name by your website site architecture.

There are several ways to set your canonical domain name, including a server-side 301 redirect. As a reminder, a 301 redirect signals a permanent redirect while a 302 redirect signals a temporary redirect.

What is a site alias or domain alias?

An alias is a second domain name linked to an existing site. The domain alias is an alternative address for the primary site. When you use an alias, the alias address is pointing to your primary domain name’s same web files but the URL does not change.

For example, if you set up an alias for example.com and myexample.com, then you would access the same content from example.com as your would from myexample.com. If you typed in example.com the browser URL would remain example.com and if you typed in myexample.com the URL would stay myexample.com in the browser; however, both URLs would show the same content. Note, the URL name does not change.

An alias is different than a redirect. Instead, a redirect automatically takes users to the primary domain. So, in the same scenario, if example.com was the primary domain then redirecting would change the typed URL of myexample.com to example.com in the browser bar.

An alias is configured in your DNS settings and can also often be found in hosting Cpanels under Domains > Aliases.

What is a domain/URL redirect?

A domain/URL redirect takes a user to a different URL, and the URL address changes in your browser. This type of redirect can be configured for a single page or an entire website.

Sometimes, a domain redirect is helpful for large companies where users might often misspell the domain name or commonly make typos. Facebook implements an advantageous example of a domain redirect. We all know the URL, facebook.com. But did you know Facebook also owns the domain facbook.com (notice the missing e)? If a user mistypes the URL and types facbook.com or faceboook.com, instead, the URL will automatically redirect a user to the intended domain name of facebook.com, instead of displaying an error.

The above Facebook redirect is an example of a redirected URL. The incorrect spelling is not constantly shown in the browser; instead, this misspelled domain name redirects the user to the correct/intended/primary domain of facebook.com.

Hint. Hint. Facebook — make a redirect for https://busines.facebook.com/ (with one “S”) to your subdomain business.facebook.com (spelled correctly with two S’s).

You might create a domain/URL redirect for many reasons; here are six reasons to create a domain redirect.

WWW/Non-WWW and HTTPS/HTTP Infographic

This infographic is intended to help clarify users’ expectations for domain names and how these expectations can be technically met via configurations, specifically regarding HTTP/HTTPS and WWW/NON-WWW URLs.

This infographic is intended to help clarify the expectations users have for domain names and how these expectations can be met technically via configurations, specifically regarding HTTP/HTTPS and WWW/NON-WWW URLs.

Best practice for server-side redirects

WWW and Non-WWW

Your website needs to support www and non-www usage. Choose which version is your primary domain and redirect the other. It’s important to address http and https usage, too.

HTTP stands for Hypertext Transfer Protocol and HTTPS stands for Hypertext Transfer Protocol Secure. HTTPS protocols use an SSL certificate for improved security. Modern sites should force the use of HTTPS.

You can redirect your www or non-www domain names and HTTP and HTTPS in many different ways. Some services and control panels provide tools for Page Rules, Forwarding, or Redirects. Alternatively, you can manually edit your domain’s virtual host, your nginx.conf, or .htaccess file directly. Linuxize has a good resource on this.

There are many rules you can use to implement your exact preference for www to non-www or vice versa, forcing HTTPS and more. I encourage you to search online for exactly what you need.

Using .htaccess

The .htaccess file is a per-directory configuration file on your Apache webserver. Aleyda Solis has a handy htaccess redirect and rewrite online script generator.

Please note that your .htaccess code only needs RewriteEngine On declared once.

Sample .htaccess domain redirect examples

Here are a few sample .htaccess redirect examples and a link to Acquia’s helpful resource for .htaccess rewrite rules.

Please note that in all examples below, make sure that “RewriteEngine On” is present in your .htaccess but don’t accidentally duplicate the “Rewrite Engine On” line anywhere in your .htacces file.

Force HTTPS for all URLS from your .htaccess file

This server-side 301 redirect rewrites all URLs to your domain to utilize HTTPS.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

htaccess redirect for www to non-www

If you chose that your canonical domain is naked (non-www) then there are several htaccess directives you can use.

# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]

This how the directive above works:

The rule redirects all requests with the “www” prefix to the non-www version of the same URL. The %1 back-reference in this RewriteRule stands for the domain name without “www”, which is captured by the parentheses (.*) in the prior line labeled as the RewriteCond. the [NC] at the end of the RewriteCond line is called a flag and makes the match case-insensitive. { HTTP_HOST } is the domain name, {Request_URI} is the part of the URL that comes after the domain name, and [L,R=301] stands for L, a flag that means it is the last rule to be processed if it is a match and R=301 stands for 301 Redirect.

This rule will correctly strip the “www” out of the the URL for all levels of your site directory and forward the request to the non-www version of the URL with the same directory structure in tact.

For example, if a user types in http://www.example.com/this-page, this rule will redirect them to https://example.com/this-page or if they type in http://www.example.com/this-directory/this-page, it will redirect them to https://example.com/this-directory/this-page.

Force URLS to HTTPS and no WWW (naked URL)

Another option is below. It uses very specific code where you need to include your specific domain. So, when you copy/paste this code example, don’t forget to replace example.com with your domain name. This code runs if either HTTP or www is included as the URL in the browser and 301 redirects the URL to the secure HTTPS protocol and strips the www, to look like https://example.com.

# Redirect www to non-www
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

Force URLS to display HTTPS and WWW

If the www version of your site is primary, copy and paste this .htaccess code to force HTTPS and the www version of your website to always show in the browser URL bar.

This code below always 301 redirects users to the https://www version of your domain.

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

Additional Canonical Updates in HTACCESS

This code uses the .htaccess file to redirect /index.html to /. This removes duplicate content as Google now knows that the URL displayed at / is the canonical content.

RewriteEngine On
RewriteRule (.*)index\.html$ /$1 [NS,R=301,L]

In addition, note that the NS flag prevents this rule being processed for subrequests (which is what prevents a redirect loop in this instance).

This snippet of code fixes the error you may see in Google Search Console that reads, “Duplicate without user selected canonical. These pages aren’t indexed or served on Google.”

Google Search Console

Relevant DNS Records for WWW Redirect

DNS records can point both www and non-www to your server’s IP address, but DNS records do not create web redirects for users. The actual physical redirect is controlled server-side. This is confusing but important to understand: the A record and CNAME configurations do not forward requests.

DNS A Record and CNAME record for WWW subdomain example
DNS A Record and CNAME record for WWW subdomain example

However, to best deal with both www and non-www visitors to your website, you’ll still want an A record and a CNAME. Both are set from within your domain’s DNS settings.

An A record for your website should be an @ (no www) pointing to your IP address.

A CNAME record or “Canonical Name” alias is common for dealing with www.example.com and example.com. A DNS CNAME record is often added for www. A CNAME record can not be used for a root domain name. A CNAME is used to point additional hosts to the same place and follow the same set of DNS rules. For instance, a CNAME record can point the web address www.example.com to the domain example.com. It’s important to note that the CNAME is pointing to another domain name, not pointing t an IP address. Your A record is the DNS record pointing to the physical IP address. Dnsimple is a great resource to learn more about this specific setting.

To note: Configuring a CNAME does not automatically result in an HTTP 301/302 redirect. The CNAME essentially says that the server for the hostname is located at the same IP address as the server for the other hostname. It’s now up to the server at that IP address to handle the requests. The web server can be configured to trigger 301/302 redirects and to serve the same or different content.

To note, redirects of www to non-www or HTTP to HTTPS are done server-side, these redirects are not made active by A Record and CNAME DNS record updates.

A caveat: some DNS services provide redirection tools that allow you to create 301/302 redirects. An example is GoDaddy.

WWW Subdomain to Non-WWW Domain on GoDaddy Hosting

On GoDaddy, to make both www and non-www domains work, you need to set up an A record, a CNAME for www pointing to @, and what GoDaddy calls a Forward. This is a GoDaddy tool created as a solution for this common request that does not require .htaccess file manipulation.

Make sure you choose Subdomain forwarding, as www is a subdomain.

Use GoDaddy's forwarding tool to redirect the subdomain www to a non-www address.

Other services provide similar tools, such as Cloud Flare’s Rules.


I hope this post helps to provide clarity. Please contact me or leave a comment with questions or further clarifications for the community. Thanks!

Happy Redirecting!

About the author

Kelly Barkhurst

Designer to Fullstack is my place to geek out and share tech solutions from my day-to-day as a graphic designer, programmer, and business owner (portfolio). I also write on Arts and Bricks, a parenting blog and decal shop that embraces my family’s love of Art and LEGO bricks!

Add comment

By Kelly Barkhurst

Recent Posts

Archives

Categories