Post by soft2u » Thu Jun 10, 2021 9:26 pm

Hello
Is there any way to make auto 301 Redirect from All Non-SEO URL to SEO friendly URL ?
I know that all URLs have a canonical SEO friendly URL , but I need to redirect the old ones to the new URLs .

Newbie

Posts

Joined
Mon Jun 07, 2021 9:36 pm

Post by rjcalifornia » Thu Jun 10, 2021 10:25 pm

soft2u wrote:
Thu Jun 10, 2021 9:26 pm
Hello
Is there any way to make auto 301 Redirect from All Non-SEO URL to SEO friendly URL ?
I know that all URLs have a canonical SEO friendly URL , but I need to redirect the old ones to the new URLs .
This post suggest a way to do it:
viewtopic.php?t=150216


Or use an extension:
https://www.opencart.com/index.php?rout ... n_id=30762

Image
A2 Hosting features: Shared Turbo Boost, Managed Warp 1, Unmanaged Hyper 1, and Warp 2 Turbo


Active Member

Posts

Joined
Fri Sep 02, 2011 1:19 pm
Location - Worldwide

Post by soft2u » Thu Jun 10, 2021 10:53 pm

No ,Unfortunately, it is not applicable
the structure of my old URL was https://site.com/index.php?route=produc ... ct_id=1811 and the new one is https://site.com/product-name
so, what I need is redirect all old URL to th the new ones
not manually one by one , but by some solution , I need to bulk redirect all old URLs in the site to the new URLs

Newbie

Posts

Joined
Mon Jun 07, 2021 9:36 pm

Post by by mona » Thu Jun 10, 2021 11:57 pm


DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.


https://www.youtube.com/watch?v=zXIxDoCRc84


User avatar
Expert Member

Posts

Joined
Mon Jun 10, 2019 9:31 am

Post by paulfeakins » Fri Jun 11, 2021 7:12 pm

If you can't find an extension, you could pay a developer such as ourselves or post a job in the Commercial Support Forum.

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Guru Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by by mona » Fri Jun 11, 2021 8:35 pm

You could create an OCMOD which adds this:
in file: catalog/controller/product/product.php
after

Code: Select all

public function index() {
add

Code: Select all

// redirect non-seo to seo if seo enabled and seo url exists
	if ($this->config->get('config_seo_url') && strstr($_SERVER['REQUEST_URI'],'product/product')) {
		if (!empty($this->request->get['product_id'])) {
			if (!empty($this->request->get['path'])) {
				$location = $this->url->link('product/product', 'path='.$this->request->get['path'].'&product_id=' . $this->request->get['product_id']);
			} else {
				$location = $this->url->link('product/product', 'product_id=' . $this->request->get['product_id']);
			}
			
			if (!stristr($location,'product/product')) {
				header("Location: $location", true, 301);
				exit;
			}
		}
	}
That code checks if the url which got you there is non-seo.
If seo is enabled and a product id is given, it creates the seo url.
If the created url is valid, it redirects to that, with or without a category path.

you could do the same for categories.

If you want ALL urls to redirect, you would need a more general solution via the seo_url class.

for all seo urls you can also do this:

in file catalog/controller/startup/seo_url.php

change this:

Code: Select all

if (!isset($this->request->get['route'])) {
				if (isset($this->request->get['product_id'])) {
					$this->request->get['route'] = 'product/product';
				} elseif (isset($this->request->get['path'])) {
					$this->request->get['route'] = 'product/category';
				} elseif (isset($this->request->get['manufacturer_id'])) {
					$this->request->get['route'] = 'product/manufacturer/info';
				} elseif (isset($this->request->get['information_id'])) {
					$this->request->get['route'] = 'information/information';
				}
			}
		}


to this:

Code: Select all

if (!isset($this->request->get['route'])) {
				if (isset($this->request->get['product_id'])) {
					$this->request->get['route'] = 'product/product';
				} elseif (isset($this->request->get['path'])) {
					$this->request->get['route'] = 'product/category';
				} elseif (isset($this->request->get['manufacturer_id'])) {
					$this->request->get['route'] = 'product/manufacturer/info';
				} elseif (isset($this->request->get['information_id'])) {
					$this->request->get['route'] = 'information/information';
				} elseif (isset($this->request->get['tag'])) {
					$this->request->get['route'] = 'product/search';
				}
			}
		} elseif (!empty($this->request->get['route'])) {
			// routes we want to check, this avoids doing seo url queries for routes that need no seo urls like account and checkout
			$valid_routes = array('product/product', 'product/category', 'product/special', 'product/manufacturer/info', 'information/information'); // extend or limit this to your needs
			if (in_array($this->request->get['route'], $valid_routes)) {
				$seo_link = $this->rewrite( $_SERVER['REQUEST_URI']);
				if (!stristr($seo_link,'route=')) {
					$this->log->write('redirecting: '.$_SERVER['REQUEST_URI'].' -> '.$seo_link);
					header("Location: $seo_link", true, 301);
					exit;
				}
			}
		}

DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.


https://www.youtube.com/watch?v=zXIxDoCRc84


User avatar
Expert Member

Posts

Joined
Mon Jun 10, 2019 9:31 am

Post by soft2u » Fri Jun 18, 2021 1:29 am

by mona wrote:
Fri Jun 11, 2021 8:35 pm

If you want ALL urls to redirect, you would need a more general solution via the seo_url class.

for all seo urls you can also do this:

in file catalog/controller/startup/seo_url.php

change this:

Code: Select all

if (!isset($this->request->get['route'])) {
				if (isset($this->request->get['product_id'])) {
					$this->request->get['route'] = 'product/product';
				} elseif (isset($this->request->get['path'])) {
					$this->request->get['route'] = 'product/category';
				} elseif (isset($this->request->get['manufacturer_id'])) {
					$this->request->get['route'] = 'product/manufacturer/info';
				} elseif (isset($this->request->get['information_id'])) {
					$this->request->get['route'] = 'information/information';
				}
			}
		}


to this:

Code: Select all

if (!isset($this->request->get['route'])) {
				if (isset($this->request->get['product_id'])) {
					$this->request->get['route'] = 'product/product';
				} elseif (isset($this->request->get['path'])) {
					$this->request->get['route'] = 'product/category';
				} elseif (isset($this->request->get['manufacturer_id'])) {
					$this->request->get['route'] = 'product/manufacturer/info';
				} elseif (isset($this->request->get['information_id'])) {
					$this->request->get['route'] = 'information/information';
				} elseif (isset($this->request->get['tag'])) {
					$this->request->get['route'] = 'product/search';
				}
			}
		} elseif (!empty($this->request->get['route'])) {
			// routes we want to check, this avoids doing seo url queries for routes that need no seo urls like account and checkout
			$valid_routes = array('product/product', 'product/category', 'product/special', 'product/manufacturer/info', 'information/information'); // extend or limit this to your needs
			if (in_array($this->request->get['route'], $valid_routes)) {
				$seo_link = $this->rewrite( $_SERVER['REQUEST_URI']);
				if (!stristr($seo_link,'route=')) {
					$this->log->write('redirecting: '.$_SERVER['REQUEST_URI'].' -> '.$seo_link);
					header("Location: $seo_link", true, 301);
					exit;
				}
			}
		}
This is an Awesome solution
Thanks a lot by mona
I only found a small problem with it, but I am trying to find it and fix it
The link is redirecting to the following format:
https://DOMAIN.COM/:///CATEGORY/SUB-CATEGORY
But thank you very much

Newbie

Posts

Joined
Mon Jun 07, 2021 9:36 pm

Post by by mona » Fri Jun 18, 2021 3:13 pm

forgot to add the scheme and host, normally done in the url class first.

change this:

Code: Select all

$seo_link = $this->rewrite( $_SERVER['REQUEST_URI']);
		if (!stristr($seo_link,'route=')) {
		$this->log->write('redirecting: '.$_SERVER['REQUEST_URI'].' -> '.$seo_link);
		header("Location: $seo_link", true, 301);
		exit;
}
to this:

Code: Select all

$full_link = (!empty($_SERVER['HTTPS']) ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; // full url creation
		$seo_link = $this->rewrite($full_link); // find the seo url
		if (!stristr($seo_link,'route=')) {
		$this->log->write('redirecting: '.$full_link.' -> '.$seo_link); // comment this out if all is well, it writes to the log
		header("Location: $seo_link", true, 301);
		exit;
}

DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.


https://www.youtube.com/watch?v=zXIxDoCRc84


User avatar
Expert Member

Posts

Joined
Mon Jun 10, 2019 9:31 am
Who is online

Users browsing this forum: No registered users and 263 guests