Tuesday 22 January 2013

CQ Redirects based on 'Locale'


There are some cases when the user needs to be redirected to the main page of the website according to his browser default language settings. For eg: if the user locale is set to 'fr' , 'French' site should open; else default English version. Next possible requirement could be, if user selects the 'en' language link , the user should be redirected to ENGLISH site from the 'fr' locale.

Let us see how to go ahead with this:

We can go for two approaches to achieve this :

    1)  JSP Redirects , [Redirect by reading the HTTP Request parameters].
   
    >   Site root node as template /components/pages/redirect
    >    Within the content.jsp of this page call some business logic to perform
    >    Now use business logic to evaluate the accept-language header and then a redirect based on language with a default language in case the header value did not cover an available language node.
   
   
    2)  Apache HTTP rewrite rule , [By using mod_rewrite and redirect based on the Accept-Language header].
 
* Say, we have a request for the root URL / then redirect to the language version of a home page with a HTTP 301

* Remember home page root directory path is domain dependent

* Use country-specific redirect for international toplevel domains such as it, de, fr

* Now accept-Language with english: serve en.html

  RewriteCond   %{HTTP:Accept-Language} ^en    

  RewriteRule   ^/$     /en.html       

  * Accept-language=COUNTRYID or no Accept-Language header or anything else: serve <lang>.html

  RewriteRule   ^/$     /de.html       


Which approach is better ?

The second approach is better compared to first. The advantage of this approach is that it reduces the load on your CQ instance significantly, because Apache can handle all requests to "/" - which is typically the most popular URL for every site/domain.

No comments:

Post a Comment