Jump to content

Prevent "renaming" of knowledgebase URLs


Recommended Posts

Let's say you have a knowledgebase article with the following URL: https://example.com/whmcs/knowledgebase/1/Create-a-backup.html
Nothing prevents anyone from linking to that page with any text, which can cause problems. Anyone could link to https://example.com/whmcs/knowledgebase/1/WHMCS-should-fix-this.html, and it would show the correct knowledgebase article, but the URL would show as "https://example.com/whmcs/knowledgebase/1/WHMCS-should-fix-this.html" instead of redirecting to the correct URL.

I made this hook a while back and thought that other people might find it useful.

<?php

add_hook('ClientAreaPageKnowledgebase', 1, function ($vars) {

	// Get the current URL and create the URL that should be redirected to
	$url = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; 
	$trueURL = $vars['systemsslurl'] . 'knowledgebase/' . ($vars['kbcurrentcat']['id'] ? $vars['kbcurrentcat']['id'] : $vars['kbarticle']['id'] ). '/' . urlencode($vars['kbcurrentcat']['urlfriendlyname'] ? $vars['kbcurrentcat']['urlfriendlyname'] : $vars['kbarticle']['urlfriendlytitle']. '.html') ; 
	
	// Only redirect if visitor is viewing a knowledgebase category or an article
	if (($vars['templatefile'] === 'knowledgebasecat' || $vars['templatefile'] === 'knowledgebasearticle') AND strpos($url,'search') === false AND strpos($url,'tag') === false) {
		if ($trueURL !== $url) {
			header('Location: ' . $trueURL);
			exit();
		}
	}

});

Feel free to use it or modify it how you'd like.

Link to comment
Share on other sites

  • 11 months later...
  • 5 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated