Forum Discussion

KaitlinSaxton's avatar
9 years ago

Strip parameters from http.request.url URL

Hi,

 

So we had an issue with gating attachments. We wanted to make sure all users had to login/register to access attachments. This worked fine until an event sent out an email with a link to an article on our community, but with tracking codes in the URL. When the user clicks an attachment, they get redirected to login, but then it tries to redirect them back the article with all the tracking codes attached. These actually cause it to error out and redirects to our support website (our server default I suppose, not sure). I want to strip out the tracking codes, which begin with "?stc=". This is the code I have:

 

<#if user.anonymous>                                                         // if user is anonymous

<#attempt> 

<@liaAddScript> 
(function($) { 
var referrer = ${http.request.url?keep_before("?stc=")};    // set referrer to the current URL but cleaned up
var loginUrl ='LOGINURL?url='+referrer;                          // append referrer to the login url for a redirect
jQuery('a.attachment-link').attr('href',loginUrl);                  // pop this login + referral URL into attachment anchors
})(LITHIUM.jQuery); 
</@liaAddScript> 

<#recover> 
</#attempt> 
</#if> 

I got the concept of gating attachments to anonymous users from here, and that works as expected. It's only now that I'm trying to modify ${http.request.url} that it isn't working at all. The sample code actually used "window.location.href" for the referral, but I don't know how I would go about removing the tracking codes from that. I also looked into js strip() but I'm not knowledgeable enough in it (or Freemarker) to figure this out. I'm currently adding this to the page wrapper.

 

Thanks,

Katie

  • Well, the way you use keep_before is correct, so what error are you seeing?

     

    You may need to add this:

    var referrer = ${http.request.url?keep_before("?stc=")?url}

    Every redirect url I've ever seen had the parameter url encoded, so https%3A%2F%2Fcommunity instead of http://community. That's what the ?url at the end does for you.

     

    Also, since, as far as I know, urls only ever have one ?, you could tweak it to just keep_before("?"). Just in case you ever change the parameters.