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.
- Ease of creation of pretty url for an asset which can be tested before go-live
- Creating url for an asset (http status is set to 200)
- Creating 301 url for an existing asset (http status is set to 301 / 302)
- 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)
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
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:
Disclaimer: The 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!!