• Home
  • Get help
  • Ask a question
Last post 7 hours 55 min ago
Posts last week 89
Average response time last week 30 min
All time posts 67735
All time tickets 10467
All time avg. posts per day 21

Helpdesk is open from Monday through Friday CET

Please create an (free) account to post any question in the support area.
Please check the development versions area. Look at the changelog, maybe your specific problem has been resolved already!
All tickets are private and they cannot be viewed by anyone. We have made public only a few tickets that we found helpful, after removing private information from them.

#7132 – Help to code dynamic SEF redirection

Posted in ‘sh404SEF’
This is a public ticket. Everybody will be able to see its contents. Do not include usernames, passwords or any other sensitive information.
Monday, 27 April 2020 13:42 UTC
ntempest
 Hi,

I have a large site with thousands of URLs in googles index.

I need to dynamically redirect some Kunena forum posts from a URL like this example:

website.com/forum/parenting-advice/513999-Re-How-do-I-choose-a-school.html
to
website.com/forum/parenting-advice/513445-How-do-I-choose-a-school.html

which means i need to strip out the "Re-"

and (this is the harder part) I need to replace 513999 (child post id) by 513445 (parent post/thread id)

which needs a database call to find the parent post id from a child post id.

I think i can write the php code for this - my question is which file in sh404SEF and in which line number in the file would this code need to go

If you had (from previous client support) an example of the code that would be amazing - but if not I will work it out

BTW in my setup i am using joomla handler for Kunena posts not default handler which is how i believe it should be set up

Many thanks in advance

Ian
Monday, 27 April 2020 15:31 UTC
wb_weeblr
Hi

As this only a redirect, there's no reason to tie this to sh404SEF. You should add a separate system plugin which runs your code on the onAfterRoute Joomla event and do the redirect at this stage.

my question is which file in sh404SEF and in which line number in the file would this code need to go
sh404SEF is a large extension with several hundreds files and ens of thousands of lines, it's not really as simple as "which file and line number" any more :)

However we do have a facility to allow code customization, it works like this:

- create or open a file called /libraries/weeblr/sh404sef_functions.php
- since last version, we include a sample file there so that file is already filled with sample customizations, all commented out
- locate the line that includes "sh404sef_before_parse_rule", around line 300 (note that there are 2 such lines, look for the second one
The code in the sample file looks like this:

//ShlHook::add(
//	'sh404sef_before_parse_rule',
//	function ($uri, $jRouter) {
//
//		$path = $uri->getPath();
//
//		$doNotAutoRedirectFolders    = array(
//			'images/Documents/'
//		);
//		$doNotAutoRedirectExtensions = array(
//			'.pdf',
//			'.mp4',
//			'.txt'
//		);
//
//		if (
//			wbStartsWith(
//				$path,
//				$doNotAutoRedirectFolders
//			)
//			&&
//			wbEndsWith(
//				$path,
//				$doNotAutoRedirectExtensions
//			)
//		)
//		{
//			throw new \Exception('Not found', 404);
//		}
//
//		//$doNotAutoRedirectList = array(
//		//	'images/Documents/test.pdf',
//		//	'images/Documents/another.pdf'
//		//);
//		//
//		//if (
//		//	wbEndsWith($path, $doNotAutoRedirectList)
//		//)
//		//{
//		//	throw new \Exception('Not found', 404);
//		//}
//
//		return $uri;
//	}
//);


Now you need to remove all the comments and you get:

ShlHook::add(
	'sh404sef_before_parse_rule',
	function ($uri, $jRouter) {

		$path = $uri->getPath();

		$doNotAutoRedirectFolders    = array(
			'images/Documents/'
		);
		$doNotAutoRedirectExtensions = array(
			'.pdf',
			'.mp4',
			'.txt'
		);

		if (
			wbStartsWith(
				$path,
				$doNotAutoRedirectFolders
			)
			&&
			wbEndsWith(
				$path,
				$doNotAutoRedirectExtensions
			)
		)
		{
			throw new \Exception('Not found', 404);
		}

		//$doNotAutoRedirectList = array(
		//	'images/Documents/test.pdf',
		//	'images/Documents/another.pdf'
		//);
		//
		//if (
		//	wbEndsWith($path, $doNotAutoRedirectList)
		//)
		//{
		//	throw new \Exception('Not found', 404);
		//}

		return $uri;
	}
);


I think you should only keep the start and remove all the rest:

$path = $uri->getPath();


will give you the path of the current request, ie

forum/parenting-advice/513999-Re-How-do-I-choose-a-school.html

Then it's up to modiyf that code to your needs. You can use the helper functions wbStartWith() if you need them. But then you should identify the URLs you want to redirect, compute the target and redirect with:

JFactory::getApplication()->redirect($targetRedirectUrl);


Best regards

Yannick Gaultier
weeblr.com
@weeblr


 
Tuesday, 12 May 2020 05:34 UTC
system
This ticket has been automatically closed. All tickets which have been inactive for a long time are automatically closed. If you believe that this ticket was closed in error, please contact us.
This ticket is closed, therefore read-only. You can no longer reply to it. If you need to provide more information, please open a new ticket and mention this ticket's number.