Sunday, July 16, 2017

Creating 301 redirect assets within WebCenter Sites

This post applies to WCS 11g 11.1.1.8.0 and higher as vanity url concept was introduced with WCS 11.8 version.

What is vanity url?
Vanity url are short, pretty and easy-to-remember explanatory url which represent a unique webpage of a site. Prior to vanity url concept, you might be familiar with either URLAssembler or GST or both to generate such urls. With the introduction of vanity url, it has become very easy for editors to define vanity urls for the asset within WCS.
There are few tricks which I would like to share among the developers to leverage and make efficient use of vanity url for their clients.

Technically, these are few key features of vanity url within WCS:
  • To the core, vanity URL within WCS is nothing but a http server filter - URLRewriteFilter which actually processes incoming vanity url requests, searches for its entry in one table - WebReferences where all vanity urls are saved and further, disassembles into long url which WCS internally further processes and corresponding output is returned.
  • WCS saves all the vanity urls within one table - WebReferences (whereas WebReferencesPatterns saves the patterns if defined any)
  • Every vanity URL is a single entry within WebReferences table irrespective of the http status set: 200, 301 or 302
  • Within WCS 12c, there is an option to set default url for an asset, if an asset has many urls.
  • Target urls for 301 / 302 can be external url too, making it very easy for editors to set other webpages as target which are NOT present within WCS without any help of network / front-end server team, which is quite useful for some customers.
What can be achieved by creating vanity url within WCS?
  1. Ease of creation of pretty url for an asset which can be tested before go-live
  2. Creating url for an asset (http status is set to 200)
  3. Creating 301 url for an existing asset (http status is set to 301 / 302)
  4. Creating 301 url for a non-existing source and non-existing target webpage (custom solution to create 301 redirect for non-existing webpage within WCS)
First 3 points are straightforward concept within WCS if you have read official document and thus, its redundant to talk about them. Lets talk about the 4th point where editors want to create 301 redirect urls for an external target webpage where target website actually is not present in WCS in form of asset. It is pretty much clear that in order to create 301 redirects (3rd point above), the source or target asset should exist within WCS. But then how to create 301 redirects without target and source being within WCS. Please note, one assumption with the explained below solution is that you want to keep your domain name fixed i.e. editors want to create 301s from fixed domain to other target webpages. For e.g. if you site runs on http://www.example.com/en/home and is using Relative webroot, then all the 301s like http://www.example.com/[any-string] would work. Thus, your client should actually know what can be achieved and what cannot be before offering them such solution.

Following are the steps on how it can be achieved with little bit of development effort:
  • Create an assettype / asset definition for e.g. say Redirects_C
  • Create 2 simple attributes: source (single, unique string) and target (single, text, optional: custom URL Attribute Editor)
  • Create a flex filter or asset listener which triggers when any new or existing Redirects_C asset is saved, which in return generates an entry within WebReferences table with following column values:
    • id = ics.genID(true)
    • webroot = [webroot for the site]
    • webreferenceurl =  source string value (without spaces)
    • assetid = [current asset id]
    • assettype = Redirects_C
    • httpstatus = 301
    • redirecturl = target text value
    • redirectwebroot = [webroot for the site]
    • template = [Main page layout] -- Doesn't matter which template is used
    • wrapper = [Site Wrapper] -- Doesn't matter whichever wrapper is used
    • isdefault = 'F'
    • modifieddate = [date when asset is saved]
    • createddate = [date when first time asset was created]
    • others leave them so that NULL value is set automatically
Enable Redirects_C assettype for your site and add startmenu for it.
Task for Editor is to just add entry for source and target's value and save the asset which triggers the custom flex filter or asset listener and generates an entry in WebReferences table. For e.g. let say your site is configured to use relative webroot - "/"; create an asset with source value as "test" and target value as "http://www.example.com/test-page" and save it, custom flex filter or asset listener should create an entry within WebReferences table. When you hit the url in browser like [http / https]: [hostname] : [port]/test, it would be redirected to http://www.example.com/test-page. As the page is redirected directly to target url which is calculated within URLRewriteFilter, adding any values for template or wrapper works. Actually, for all 301 / 302 redirects, WCS never processes anything if the target is external webpage of another site.

Following is sample snippet of code on how to create an entry within WebReferences table for Redirects_C asset:

DisclaimerThe code and/or the configurations posted are not official recommendations and should be used at sole's discretion with proper testing before deploying on live WebCenter Sites systems. Cheers!! 

A simple code compare functionality

One of the most important aspect of any development cycle is deployment and while deployment, it is very important to note the changes don...