Announcement

Collapse
No announcement yet.

How to change external links to internal links to do something before redirecting to the destination

Collapse
This is a sticky topic.
X
X
Collapse
First Prev Next Last
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Free Mod How to change external links to internal links to do something before redirecting to the destination

    There are cases when you want external links posted in your forum to go through your page first before redirecting to the external URL. Most sites do this. Here are the reasons why you would want to do this:

    1. To track the clicks to the external links.
    2. To display an interstitial page to inform/warn user that they are navigating away from your site.

    Click image for larger version  Name:	redirect-min.png Views:	1 Size:	14.3 KB ID:	1943

    See how Yelp does #2.
    Without further ado, here's how to do it.

    There are 2 parts to do this:
    • Creating the redirect page. This is only for #2 above. For #1, you're on your own. If you already have set it up, you can skip this)
    • Editing the php file to change the external links to point to the redirect page.

    Create the Redirect Page:

    1. Create a new page in Sitebuilder.
    2. Drag and drop a PHP module.
    Title: Redirecting... (or whatever you want)
    PHP Code: (Change 5000 milliseconds to whatever delay you want)
    PHP Code:
    $url = isset($_GET['url']) ? urldecode($_GET['url']) : '';
    if (
    $url) {
        echo 
    '<p>You are leaving this site and will be automatically redirected to <a href="' htmlspecialchars($url) . '" id="vbmodsthatrock-external-link" rel="nofollow" onclick="return redirect(this.href);">' htmlspecialchars($url) . '</a> in a moment.</p>
            <script>
                function redirect(url) {
                    location.replace(url);
                    return false;
                }
                // redirect to the external URL with a 5-second delay
                setTimeout(function() {
                    redirect(document.getElementById("vbmodsthatrock-external-link").href);
                }, 5000); // 5000 milliseconds = 5 seconds, change accordingly
            </script>'
    ;
    }
    else {
        echo 
    'Sorry, there is no link to redirect to. You must have followed an invalid link.';


    3. Save the page and fill out the page information.
    Save the page name: Redirecting... (or whatever you want to show up in the tab name)
    Save the page URL based on the page name: redirect (this is what you're going to use when editing the PHP file)
    Template option: Redirect Template


    Edit the PHP File:

    1. Logon to cPanel (or equivalent).
    2. Edit the following PHP file:
    Code:
    /includes/vb5/template/bbcode.php
    3. Change these:
    PHP Code:
    // standard URL hyperlink
    return "<a href=\"$rightlink\" target=\"_blank\"" . ($is_external ' rel="nofollow"' '') . ">$text</a>"

    with these: (change $redirect_path with the appropriate path of the redirect URL)
    PHP Code:
    $rightlink_host parse_url($rightlinkPHP_URL_HOST);
    $forum_baseurl vB::getDatastore()->getOption('frontendurl');
    $forum_host parse_url($forum_baseurlPHP_URL_HOST);        

    // if same host, then use the link directly
    if (vB5_String::stripos($rightlink_host$forum_host) !== false) {
        return 
    '<a href="$rightlink" target="_blank"' . ($is_external ' rel="nofollow"' '') . ">$text</a>";
    }
    else { 
    // link to your redirect page and pass the encoded external link

        // specify path of the redirect URL (exclude forum base url) including the querystring parameter name
        // this is the path you used when you created the redirect page in Sitebuilder
        
    $redirect_path "/redirect?url=";

        return 
    '<a href="' $forum_baseurl $redirect_path urlencode($rightlink) . '" target="_blank"' . ($is_external ' rel="nofollow"' '') . ">$text</a>";


    4. Clear system cache in AdminCP > Maintenance > Clear System Cache.


    Screenshot:

    Click image for larger version  Name:	redirect-page-min.png Views:	1 Size:	25.0 KB ID:	1963

    NOTE: Since this is a PHP hack, this change will be overwritten during upgrade. So you have to re-apply this change whenever you upgrade.
    Last edited by glennrocksvb; 10-02-2019, 02:45 PM. Reason: Updated code to escape "\" in posts to avoid bug in vB
    Helpful? Donate. Thanks!
    Buy me a coffeePayPal QR Code
    Fast VPS Host for vBulletin:
    A2 Hosting & IONOS

    #2
    Hello,
    1: i try do that change this ( what i have in my
    /includes/vb5/template/bbcode.php )
    HTML Code:
            // standard URL hyperlink
            return "<a href="$url" target="_blank">$text</a>";
        }
    with these:

    HTML Code:
    $rightlink_host = parse_url($rightlink, PHP_URL_HOST);
    $forum_baseurl = vB::getDatastore()->getOption('frontendurl');
    $forum_host = parse_url($forum_baseurl, PHP_URL_HOST);        
    
    // if same host, then use the link directly
    if (vB5_String::stripos($rightlink_host, $forum_host) !== false) {
        return "<a href="$rightlink" target="_blank"" . ($is_external ? ' rel="nofollow"' : '') . ">$text</a>";
    }
    else { // link to your redirect page and pass the encoded external link
    
        $redirect_path = "/link.php?url="; // specify path of the redirect URL (exclude forum base url) including the querystring parameter name
    
        return "<a href="" . $forum_baseurl . $redirect_path . urlencode($rightlink) . "" target="_blank"" . ($is_external ? ' rel="nofollow"' : '') . ">$text</a>";
    }
    Clear system cache ... after that my site not working !
    2: can you please more clarification about this (change $redirect_path with the appropriate path of the redirect URL)
    Thank You

    Comment


      #3
      for example this my goodbye page

      were should add in second HTML Code:?

      Comment


        #4
        Could you elaborate on "my site not working"? What does not work? Saying "not working" does not help.

        Btw, there is a bug in vB where the backslash character "\" in the post goes away after posting or editing. I had to double the backslash to be able to show one. Make sure you copied the code in the first post correctly with backslashes preserved.
        Helpful? Donate. Thanks!
        Buy me a coffeePayPal QR Code
        Fast VPS Host for vBulletin:
        A2 Hosting & IONOS

        Comment


        • William
          William commented
          Editing a comment
          I think he is referring to the instructions: with these: (change $redirect_path with the appropriate path of the redirect URL)

          Would this be correct in his case: $redirect_path = &quot;/test/22.html&quot;;

          ???

        #5
        attached my original bbcode.php
        change to me and add redirect page http://shneler.com/test/22.html
        i will test and back to you quickly
        Attached Files

        Comment


        • glennrocksvb
          glennrocksvb commented
          Editing a comment
          See post #7

        #6
        http://shneler.com/vb/
        Last edited by moded; 11-15-2016, 02:46 PM.

        Comment


        • glennrocksvb
          glennrocksvb commented
          Editing a comment
          What is this URL for?

        #7
        Originally posted by William View Post
        I think he is referring to the instructions: with these: (change $redirect_path with the appropriate path of the redirect URL)

        Would this be correct in his case: $redirect_path = "/test/22.html";
        That's correct. But I wonder what's in that 22.html file. It should have the code to redirect to the external URL passed in the querystring.
        Helpful? Donate. Thanks!
        Buy me a coffeePayPal QR Code
        Fast VPS Host for vBulletin:
        A2 Hosting & IONOS

        Comment


          #8
          Originally posted by glennrocksvb View Post
          Btw, there is a bug in vB where the backslash character "\" in the post goes away after posting or editing.
          FYI. I posted a bug report for this in http://tracker.vbulletin.com/browse/VBV-16703
          Helpful? Donate. Thanks!
          Buy me a coffeePayPal QR Code
          Fast VPS Host for vBulletin:
          A2 Hosting & IONOS

          Comment


            #9
            why when i follow the above steps i see blank page ...and were is the html code should add in redirect page ?

            Comment


              #10
              Originally posted by glennrocksvb View Post
              FYI. I posted a bug report for this in http://tracker.vbulletin.com/browse/VBV-16703
              Yes i can't editing the post , when i do that my forum frozen

              Comment


                #11
                Originally posted by moded View Post
                why when i follow the above steps i see blank page ...and were is the html code should add in redirect page ?
                When do you see the blank page? After clicking the link that should redirect to the external link? Or when loading the thread page itself? Please be more specific.

                Btw, this thread does not include the implementation of the redirect page itself. This is up to you. As I mentioned in the first, there are 2 possible ways to implement this. And the second way is how Yelp does it as shown in the screenshot I attached. But this thread does not show how to implement any of those ways. What this thread shows is how to change the external links in the posts to internal links in order for you to implement any of those 2 ways.
                Helpful? Donate. Thanks!
                Buy me a coffeePayPal QR Code
                Fast VPS Host for vBulletin:
                A2 Hosting & IONOS

                Comment


                • William
                  William commented
                  Editing a comment
                  Would be nice to include if possible a how to create an article page using Vb 5 and making that a redirect page. Not trying to create more work for you Glenn, but it just makes sense to me. That is, if it is possible to use the cms.

                • glennrocksvb
                  glennrocksvb commented
                  Editing a comment
                  You have a point. You can create a new page in vB5 and then include an HTML module to display a message and add a script to redirect to the external URL after a certain delay or after clicking a button or link. I will post detailed instructions on that when I get a chance.

                #12
                I have edited the first post to add instructions on how to create the redirect page.
                Helpful? Donate. Thanks!
                Buy me a coffeePayPal QR Code
                Fast VPS Host for vBulletin:
                A2 Hosting & IONOS

                Comment


                • William
                  William commented
                  Editing a comment
                  Do you still need to do this: with these: (change $redirect_path with the appropriate path of the redirect URL)

                  If we are using the vb cms as the redirect page? What would the url be if I did this mod for my site? I read this and about to give a try, I just wanted to be crystal clear beforehand: Save the page URL based on the page name: redirect (this is what you're going to use when editing the PHP file)
                  Last edited by William; 11-15-2016, 11:38 PM.

                #13
                I feel like an idiot, I didn't realize there were multiple places to change the code!!!!

                You said: with these: (change $redirect_path with the appropriate path of the redirect URL)


                Enjoy,
                William
                Last edited by William; 11-16-2016, 12:33 AM.

                Comment


                • glennrocksvb
                  glennrocksvb commented
                  Editing a comment
                  Glad you figured it out.
                  And thanks for the cream and sugar

                #14
                Thank you Glenn and William it is working perfect
                here test
                can you add to above PHP module , redirect the user when he ask to download attached file , already upload in forum root ?

                Last edited by moded; 11-16-2016, 08:46 AM.

                Comment


                  #15
                  What's the best way to track someone clicking what link and resulting redirect page? I am watching Google Analytics "live" and seeing people being redirected to the page. This is a wonderful addition if it can be tracked from an admin's point of view. So often it seems that someone is just replying to a title rather than the article in a discussion. It would be quite nice to be able to verify this .... . as it would save me much breath in ignoring such users that I see ignoring the article.

                  Also is it possible to have a "proceed offsite" and "go back to thread" buttons in case someone doesn't want to be redirected out of the site?

                  Enjoy,
                  William
                  Last edited by William; 11-16-2016, 01:25 PM.

                  Comment


                  • glennrocksvb
                    glennrocksvb commented
                    Editing a comment
                    What kind of tracking do you want? Track who clicks what link and when? And then count the clicks per link?

                Users Viewing This Page

                Collapse

                There is 1 user viewing this forum topic.

                • Guest Guest

                Latest Posts

                Collapse

                Working...
                X
                Searching...Please wait.
                An unexpected error was returned: 'Your submission could not be processed because you have logged in since the previous page was loaded.

                Please push the back button and reload the previous window.'
                An unexpected error was returned: 'Your submission could not be processed because the token has expired.

                Please push the back button and reload the previous window.'
                An internal error has occurred and the module cannot be displayed.
                There are no results that meet this criteria.
                Search Result for "|||"