Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions core/components/seosuite/src/Plugins/Redirects.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class Redirects extends Base
*/
public function onPageNotFound()
{
//$request = urldecode(trim($_REQUEST[$this->modx->getOption('request_param_alias', null, 'q')], '/'));
$request = $this->getFullUrl();
if (!empty($request)) {
$this->redirect($request);
$fullUrl = $this->getFullUrl();
$alias = $this->getPageAlias();
if (!empty($fullUrl)) {
$this->redirect($fullUrl, $alias);
}
}

Expand All @@ -25,28 +25,22 @@ public function onPageNotFound()
* @access protected.
* @param String $request.
*/
protected function redirect($request)
protected function redirect(string $request, $alias = '')
{
$criteria = $this->modx->newQuery(SeoSuiteRedirect::class, [
'active' => 1,
[
'old_url' => $request,
// 'OR:old_url:=' => preg_replace('#^\w{2}/#', '', $request),
// 'OR:old_url:=' => $this->getFullUrl()
'old_url' => $request,
'OR:old_url:=' => $alias,
],
/*
[
'context_key' => '',
'OR:context_key:=' => $this->modx->context->get('key')
],
*/
]);

$criteria->sortby('context_key', 'DESC');

foreach ($this->modx->getIterator(SeoSuiteRedirect::class, $criteria) as $redirect) {
$redirect->set('visits', (int) $redirect->get('visits') + 1);
$redirect->set('last_visit', date('Y-m-d H:i:s'));
$redirect->set('old_url', $request);

if ($redirect->save()) {
if (is_numeric($redirect->get('new_url'))) {
Expand All @@ -55,8 +49,8 @@ protected function redirect($request)
$url = $redirect->get('new_url');
}

if (strpos($url, 'www') === 0) {
$url = 'http://' . $url;
if (str_starts_with($url, 'www')) {
$url = 'https://' . $url;
}

if (!empty($url)) {
Expand Down Expand Up @@ -119,12 +113,18 @@ protected function shouldLog404($request)
return true;
}

protected function getFullUrl()
protected function getFullUrl(): string
{
$protocol = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . '://';
$host = $_SERVER['HTTP_HOST'];
$request = $_SERVER['REQUEST_URI'];
$resource = $_SERVER['REQUEST_URI'];

return $protocol . $host . $request;
return $protocol . $host . $resource;
}

private function getPageAlias(): string
{
$aliasParam = $this->modx->getOption('request_param_alias', null, 'q');
return urldecode(trim($_REQUEST[$aliasParam], '/'));
}
}