Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Vincent 75 posts 98 karma points
    Jun 15, 2010 @ 18:26
    Vincent
    0

    Redirect to different "homepage" using localization

    Hi, I'm new to Umbraco, I recently did some test and understand how to work with DocumentType and Macro. I also understand how to work with usercontrol and c#. Now what I want to do is on the home page, look in a db using visitor's ip to see where is from and redirect to a specific page depending on my result. Can you provide me an exemple on how I could do that please? I just don't know what would be the best way of executing this code first and redirect to one of the 3 homepages.

    Thanks a lot!

     

    PS : I think Umbraco rules, I worked with lots of CMS and none was killing it like this one! (hope to see it on MVC soon!)

  • dandrayne 1138 posts 2262 karma points
    Jun 15, 2010 @ 18:35
    dandrayne
    0

    Hi Vincent, and welcome to the forums!

    Here's something I've used to redirect visitors based on IP.  This was to redirect internal network traffic to a different page

    On the homepage I had two properties:  internalIPAddresses for a list of IPs to check against, and "internalHomepage", which was a contentpicker with the page to redirect to.  I also hard-coded a regex for an ip range - I got the regex from the tool at http://www.google.com/support/analytics/bin/answer.py?hl=en&answer=55572

    Here's the xslt - post back if you want me to go through it in more detail (it's 5:30pm here and i'm leaving!). 

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:vb="urn:the-xml-files:xslt-vb"
    xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
    exclude-result-prefixes="msxsl umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">



    <xsl:param name="currentPage"/>
    <xsl:variable name="clientIP" select="umbraco.library:RequestServerVariables('REMOTE_ADDR')"/>
    <xsl:variable name="internalIPAddresses" select="$currentPage/data [@alias='internalIPAddresses' ]"/>


    <msxsl:script language="VB" implements-prefix="vb">
    <msxsl:assembly name="System.Web"/>
    <![CDATA[
    Public Function Redirect(url As String)
    System.Web.HttpContext.Current.Response.Status = "301 Moved Permanently"
    System.Web.HttpContext.Current.Response.AddHeader("Location", url.ToString)
    Return ""
    End Function
    ]]>
    </msxsl:script>

    <xsl:variable name="internalHomepage" select="$currentPage/data [@alias = 'internalHomepage']"/>


    <xsl:template match="/">
    <xsl:if test="string($internalHomepage) != '' and string($internalIPAddresses) != '' and (
    Exslt.ExsltRegularExpressions:test($clientIP, '^129\.215\.(0\.([1-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))|(([1-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-4]))\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5])))|255\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-4])))$')
    or Exslt.ExsltRegularExpressions:test($clientIP, '^194\.81\.240\.([1-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-4]))$')
    or contains($internalIPAddresses, $clientIP))">
    <xsl:value-of select="vb:Redirect(umbraco.library:NiceUrl($internalHomepage))"/>
    </xsl:if>

    </xsl:template>




    </xsl:stylesheet>

    p.s. script inside xslt is not best practice! 

    Dan

     

  • Vincent 75 posts 98 karma points
    Jun 15, 2010 @ 19:29
    Vincent
    0

    Thanks Dandrayne for the reply!

     

    Is ther a way of doing this only in C#/VB since you say it yourself that having script inside XSLT is not a best practice? And how do I do to make the code execute on the load of the first page?

     

    Thaks again!

  • dandrayne 1138 posts 2262 karma points
    Jun 15, 2010 @ 23:00
    dandrayne
    0

    Hi Vincent

    It's best (iirc - i'm front-end!) to create an xslt extension, but using something like the above won't be a major problem.

    As for making it run on page load; if you use the above in a macro and place it anywhere on your masterpage/template it will run on page load.

    Dan

  • Vincent 75 posts 98 karma points
    Jun 16, 2010 @ 04:15
    Vincent
    0

    Thanks Dan, I'll try this later in my project and I'll let you know if I have questions but it seems pretty clear!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies