Close menu Resources for... William & Mary
W&M menu close William & Mary

Vanity 404 Redirects

Deep folder content

We prefer to organize content within a hierarchical (also called rooted tree) folder structure. Using this paradigm, we are able to organize a large quantity of content with relatively few root-level folders. One drawback is we generate arbitrarily-long URLs for content deeper in the hierarchy.

Overview of our Vanity 404 Redirects system

Each Cascade Site (except Arts & Sciences) has its own front-end web server and handles its own webserver errors — such as requests for pages that don't exist. We take advantage of this by intercepting the infamous 404 (Not Found) error to perform a bit of magic.

Before sending the remote browser a 404 error, we first check a master list of server-wide vanity URLs. For instance, requesting the following should give you a 404 error, because it's not a folder in Cascade:

But before sending the error, our Vanity 404 Redirects system checks its list of mapping rules and finds the above path is a vanity name for the Parkling and Transportation website:

The former is obviously much easier to communicate and remember than the latter. (How many Ls are in auxiliary?)
We still have a 404 error page

If the Vanity 404 Redirects system does not find a rule matching the incoming request, it sends a nice pretty 404 (Not Found) error page to the remote browser. On that page we helpfully embed Google Search results based on the incoming path.

The Vanity 404 Redirects folder in Cascade

In each Cascade Site you have a root folder named 404. Within this folder you will find:

  • mapping.php — which you will edit
  • several other files — which you will NOT edit
Description of mapping.php

The mapping.php file contains one line of PHP code for every supported Vanity 404 Redirect rule. (It also contains a bunch of potentially-helpful comments.) The one line of code for each rule "maps" an incoming path to an outgoing (redirected) path. You don't need to be an expert at PHP to edit mapping.php!

Before we analyze a sample Vanity 404 Redirect rule encoded in a line of PHP code, let's first figure out the information we'll need for the Vanity 404 Redirect rule:

  • choose a vanity path that would be an error because it doesn't exist in Cascade
    • e.g., /transportation/
  • provide a target path to some actual content that does exist in Cascade
    • e.g., /offices/auxiliary/parkingandtransportation/
  • figure out whether this rule is stable for a while or is likely to change soon or often
    • e.g., the above is a stable long-term redirect
Instructions for mapping.php

You can open and edit the 404 mapping.php file within Cascade. After making changes, you can Submit and Publish the file just like any other in Cascade.

The editor in Cascade color-codes the PHP to help you know if you've made a mistake. Hint: if your new line of code has different colors than the others, or if the new rule makes everything after it change color, you may have missed something as small as a quote or semi-colon symbol.

Here's a Vanity 404 Redirect rule:

  •  $map_path['/transportation/'] = '/offices/auxiliary/parkingandtransportation/';

Let's break it down:

  • $map_path
    • the variable name that holds stable redirects
    • more volatile redirects use the variable name $temp_path
  • ['/transportation/']
    • the vanity (incoming) path
    • this does not exist in Cascade
  • '/offices/auxiliary/parkingandtransportation/'
    • the outgoing redirected path
    • this does exist in Cascade
  • syntax and punctuation
    • everything from the $ (dollar sign) to the ; (semicolon) — including the brackets and pairs of single quote symbols — are significant
    • omitting any of these symbols will break the Vanity 404 Redirects system
More Examples

As I write this, each of the following is an actual rule within the 404 mapping.php file on www.wm.edu

  • Stable Redirects
    • $map_path['/elearning/'] = '/offices/stli/digital-learning/';
    • $map_path['/hazing/report/'] = '/sites/hazingprevention/reportinghazing/';
    • $map_path['/health/'] = '/offices/wellness/healthcenter/';
    • $map_path['/orchestra/'] = '/as/music/ensembles/symphony/';
    • $map_path['/raftdebate/'] = '/as/graduate/graduate-center/raftdebate/';
    • $map_path['/wrenchapel/'] = '/about/history/historiccampus/wrenchapel/';
  • Volatile Redirects (new or subject to change soon/often)
    • $temp_path['/academicadvising/'] = '/as/ouaa/advising/';
    • $temp_path['/deanofstudents/'] = '/about/administration/senioradmin/studentaffairs/';
    • $temp_path['/orientation/'] = '/offices/step/new-students/orientation/';

Notice the second stable example above has 2 path elements in the brackets ['/hazing/report/']. The path can be as short or long as you like. In this case, the vanity name for /sites/hazingprevention/ is /hazing/. Instead of providing them a second root level vanity name for reporting hazing, we made /report/ look look like a child folder inside their top vanity name /hazing/. Of course, neither of these exist in Cascade. Only the path on the right exists in Cascade.

TMI

The Vanity 404 Redirects system and mapping.php handle more complex rules that have been known to cause (literal) headaches when stared at too long. I'll briefly explain what they are and give a single example for when we might use such a rule. Feel free to contact [[creative]] if you come across a scenario you think might benefit from deep magic.

Use the variable names $map_deep or $temp_deep (in place of $map_path and $temp_path) to provide wildcard matching. In these examples, any incoming path elements beyond the matched rule will be appended to the outgoing path.

Here's a genuine example:

  • Student Insurance
    • The entire /studentinsurance/ folder moved from within the Health Center website /offices/wellness/healthcenter/ to become its own website under /sites/
      • All of the folders and content within /studentinsurance/ remained identical — only the first part of the path changed.
    • Here's their rule
      • $map_deep['/offices/wellness/healthcenter/studentinsurance/'] = '/sites/studentinsurance/';
    • Let's say someone finds and clicks on an old link in a search result or browser bookmark
      • /offices/wellness/healthcenter/studentinsurance/plan/referrals/
    • The rule matches the first 4 segments
      • /offices/wellness/healthcenter/studentinsurance/
    • This leave 2 extra segments we'll save for later
      • /plan/referrals/
    • The rule has an outgoing (redirect) path
      • /sites/studentinsurance/
    • The deep magic tacks on those 2 extra segments we saved for later
      • /sites/studentinsurance/plan/referrals/
    • That's a real path matching one in Cascade
      • if it were not, the browser would receive a 404 error/search page

This is obviously a powerful capability with many more use cases than we can ennumerate here. Again, feel free to contact [[creative]] if you come across a scenario you think might benefit from deep magic.