• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • About
    • Stephen Foskett
      • My Publications
        • Urban Forms in Suburbia: The Rise of the Edge City
      • Storage Magazine Columns
      • Whitepapers
      • Multimedia
      • Speaking Engagements
    • Services
    • Disclosures
  • Categories
    • Apple
    • Ask a Pack Rat
    • Computer History
    • Deals
    • Enterprise storage
    • Events
    • Personal
    • Photography
    • Terabyte home
    • Virtual Storage
  • Guides
    • The iPhone Exchange ActiveSync Guide
      • The iPhone Exchange ActiveSync Troubleshooting Guide
    • The iPad Exchange ActiveSync Guide
      • iPad Exchange ActiveSync Troubleshooting Guide
    • Toolbox
      • Power Over Ethernet Calculator
      • EMC Symmetrix WWN Calculator
      • EMC Symmetrix TimeFinder DOS Batch File
    • Linux Logical Volume Manager Walkthrough
  • Calendar

Stephen Foskett, Pack Rat

Understanding the accumulation of data

You are here: Home / Everything / Enterprise storage / How To Force Apache To Redirect To Canonical Hostnames, or ServerAlias Is Not Your Friend

How To Force Apache To Redirect To Canonical Hostnames, or ServerAlias Is Not Your Friend

August 1, 2010 By Stephen 12 Comments

Yesterday I discussed how to set up a lightweight PHP web server using Apache. Next we have to get everything running smoothly, and I came up against a frustrating realization: Apache doesn’t have a satisfying way to redirect multiple domains to canonical hostnames! In other words, it’s fairly easy to redirect one domain’s content from “www.example.com” to just plain “example.com” or to make both hostnames work, but there’s no one-stop solution to do this with a dozen domains. But I’ve hit on a method that correctly redirects alternate hostnames and will save you aggravation in the long run.

The Easy Way

Apache is one amazingly flexible web server. It handles multiple domains with ease, using the VirtualHost method, and the ServerAlias directive allows you to host permutations easily. Consider the following totally made-up example:

<VirtualHost *:80>
  ServerName blog.fosketts.net
  ServerAlias www.blog.fosketts.net
  ServerAlias fosketts.net
  ServerAlias www.fosketts.net
  DocumentRoot /var/www/
</VirtualHost>

This looks great, right? It tells Apache to watch any IP on port 80 for an HTTP request for a server called blog.fosketts.net and to serve it the content from /var/www/. It also tells Apache to accept plain old “fosketts.net”, “www.fosketts.net”, and even “www.blog.fosketts.net”.

What’s Wrong With Easy?

Although accepting all these hostnames seems like the friendly and correct thing to do, it’s not in your best interest. It tells web clients that the exact same content lives on four different servers, and they’ll start linking to your content every which way. Pretty soon you’ll have incoming links for all four hostnames. So what’s wrong with this?

  1. It’s confusing for users – They’ll start asking, “is your site www.fosketts.net or blog.fosketts.net?” It’s fine to segment things, but confusing to do it unnecessarily.
  2. It’s hard to configure and maintain – Once your site starts getting linked to and shared around, you’re stuck supporting all possible combinations. When you switch hosts or server platforms (ahem) you have to make sure everything still works.
  3. It hurts your search ranking – You might not be all that concerned with search engine placement, and it’s not as bad as some say, but splitting your traffic between multiple sites also splits your “SEO juice”.
  4. Web crawls overload your servers – Search engines treat each host name as a different server. If you allow links to multiple names without a proper redirect, you’ll get multiple crawls, often at the same time.

In summary, the easy was isn’t good. ServerAlias looks friendly, but it’s not a friend when used this way.

Let’s say your name was Stephen, but some people call you Steve. Rather than insist on one or the other, you could just go through life accepting either. But imprecision can lead to issues, even in the real world. Will people know to look up Stephen in the company directory when they know you as Steve? You might start getting duplicate junk mail for both names as they find their way onto mailing lists. Then there’s the embarrassing “I always called him Steve” moment at the company party, when someone feels like they’re not part of the “in crowd” that knows your real name. It’s best to be friendly and accept anything but politely suggest that everyone uses just one name in the interest of sanity.

Redirection is Right

The best approach in life is also the correct method on the web. Your server should be set to accept any number of possible names in case someone comes in with the wrong one. But rather than blithely accepting the name, your server should issue a proper “redirect” call, instructing the browser or crawler to reload the page using the correct name from that point on.

This is simple when using Lighttpd. I just added the following lines to my lighttpd.conf file and it magically issued a proper redirect whenever someone came in using the “www” name:

$HTTP["host"] =~ "^www\.(.*)$" {
  url.redirect  = (
    "^/(.*)" => "http://%1/$1",
  )
}

I was amazed that I could locate no such universal redirect option in Apache. You can do all the RedirectMatch calls you want, but their regular expressions only operate on the path part of the URL, not the hostname. This is great for adding a “www” but makes it impossible to create a generic rule to eliminate them!

Instead, we have to use RedirectMatch on each VirtualHost domain individually. This also opens the possibility to deal with other conditions we might come across, but it’s not as simple and clean as the Lighttpd method.

Here’s where the magic is. Each VirtualHost configuration you add (in /etc/apache2/sites-available on Ubuntu) should include rules to deal with the incorrect names as well as the single correct one. Here’s the correct redirect rule for the example above:

<VirtualHost *:80>
  ServerName fosketts.net
  ServerAlias www.fosketts.net
  ServerAlias www.blog.fosketts.net
  RedirectMatch 301 (.*) http://blog.fosketts.net$1
</VirtualHost>

<VirtualHost *:80>
  DocumentRoot /var/www/
  ServerName blog.fosketts.net
</VirtualHost>

The first VirtualHost block matches all the incorrect hostnames and redirects them (with a code of 301 for “Permanent”) to the correct hostname. The “(.*)” part matches any and all paths and arguments and the “$1” part appends them to the new hostname. Then we set up another VirtualHost block for only the correct hostname and put any and all rules in there.

This way, any clients or crawlers that hit “www.fosketts.net” or any of the other alternatives will get a proper 301 redirect to “blog.fosketts.net” and go about its business. It tells Google that there is only one proper server name for this content and encourages users (who will likely copy and paste from the address bar) to use it, too. Neat and tidy, and very friendly.

I’d love to hear alternative methods of doing this. Please leave a comment if you have a suggestion that uses a 301 redirect and works across multiple domains!

You might also want to read these other posts...

  • How To Connect Everything From Everywhere with ZeroTier
  • Liberate Wi-Fi Smart Bulbs and Switches with Tasmota!
  • Electric Car Over the Internet: My Experience Buying From…
  • Tortoise or Hare? Nvidia Jetson TK1
  • How To Install ZeroTier on TrueNAS 12

Filed Under: Enterprise storage, Everything Tagged With: Apache, Google, HTTP, lighttpd, redirect, SEO, ServerAlias, VirtualHost

Primary Sidebar

Data will expand to fill all available storage capacity

Stephen Foskett

Subscribe via Email

Subscribe via email and you will receive my latest blog posts in your inbox. No ads or spam, just the same great content you find on my site!
 New posts (daily)
 Where's Stephen? (weekly)

Download My Book


Download my free e-book:
Essential Enterprise Storage Concepts!

Recent Posts

How To Install ZeroTier on TrueNAS 12

February 3, 2022

Scam Alert: Fake DMCA Takedown for Link Insertion

January 24, 2022

How To Connect Everything From Everywhere with ZeroTier

January 14, 2022

Electric Car Over the Internet: My Experience Buying From Vroom

November 28, 2020

Powering Rabbits: The Mean Well LRS-350-12 Power Supply

October 18, 2020

Tortoise or Hare? Nvidia Jetson TK1

September 22, 2020

Running Rabbits: More About My Cloud NUCs

September 21, 2020

Introducing Rabbit: I Bought a Cloud!

September 10, 2020

Remove ROM To Use LSI SAS Cards in HPE Servers

August 23, 2020

Test Your Wi-Fi with iPerf for iOS

July 9, 2020

Symbolic Links

    Featured Posts

    Why Buy a NEX-7? Why Sony NEX At All?

    October 17, 2011

    SMB 3 is Going to be Huge, in both Scope and Impact

    May 6, 2012

    How Will Cisco Recover From The Consumer Strategy Blunder?

    January 2, 2013

    Mac OS X Lion Adds CoreStorage, a Volume Manager (Finally!)

    August 4, 2011

    Datacenter History: Through the Ages in Lego

    October 22, 2013

    Review: Blue Snowball USB Microphone

    March 31, 2010

    Introducing Rabbit: I Bought a Cloud!

    September 10, 2020

    The Best Mac OS X Terminal Font: Glass TTY VT220

    October 6, 2015

    What You See and What You Get When You Follow Me

    May 28, 2019

    My 2012 Project: Improving Energy Efficiency

    January 3, 2012

    Footer

    Legalese

    Copyright © 2022 · Log in