Post by Skyhigh » Sat May 14, 2011 5:01 am

So the following category exists in my store:
http://blahblah.com/Clothing/Mens/Jumpers/

but if I request:
http://blahblah.com/Jumpers/

then it shows the category content from:
http://blahblah.com/Clothing/Mens/Jumpers/


So, as my brains a bit fried from some Mod_Rewrite funkyness:
1. Does the current version of Opencart do this? (there's a risk I may have possible introduced this error as my version of OC is heavily modded)
2. If it is part of OC, does anyone already have a fix written to enforce category URLs? *


* My brain has exploded from too much mod_rewrite...and I'm hoping someone else has 'fixed' this feature to save me a bit of time from writing it myself in a mostly-braindead state :)
Last edited by Skyhigh on Sun May 15, 2011 5:40 pm, edited 1 time in total.

LoveMoissanite.com - Moissanite Rings - Proudly Powered by Opencart
[How To] Speed Up Page Content with Opencart - Opencart advocate since 2009


New member

Posts

Joined
Fri Sep 11, 2009 8:12 pm

Post by Skyhigh » Sat May 14, 2011 5:56 am

Ok, think I've sorted it.

Please note, I've modified catalog\controller\common\SEO_URL.php from version 1.4.9.3 - just copy paste the entire contents below into the file (after making a backup).

Code: Select all

<?php
class ControllerCommonSeoUrl extends Controller {
	public function index() {
		if (isset($this->request->get['_route_'])) {
			$parts = explode('/', $this->request->get['_route_']);
			
			foreach ($parts as $part) {
				$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($part) . "'");
				
				if ($query->num_rows) {
					$url = explode('=', $query->row['query']);
					
					if ($url[0] == 'product_id') {
						$this->request->get['product_id'] = $url[1];
					}
				
					if ($url[0] == 'category_id') {
						if (!isset($this->request->get['path'])) {
							$this->request->get['path'] = $url[1];
						} else {
							$this->request->get['path'] .= '_' . $url[1];
						}
					}	
	
					if ($url[0] == 'manufacturer_id') {
						$this->request->get['manufacturer_id'] = $url[1];
					}
					
					if ($url[0] == 'information_id') {
						$this->request->get['information_id'] = $url[1];
					}	
				} else {
					$this->request->get['route'] = 'error/not_found';
						return;	// If ANY part of the URL fails, RETURN so we show a 404
								// otherwise, if the NEXT part of the url is valid, we 'skip' the 404! Bad.
				}
			}
			

			
			if (isset($this->request->get['product_id'])) {
				$this->request->get['route'] = 'product/product';
			} elseif (isset($this->request->get['path'])) {
				if($this->VerifyCategoryPath($this->request->get['path']) == true){
					$this->request->get['route'] = 'product/category';
				}
				else{
					$this->request->get['route'] = 'error/not_found';	
				}
			} elseif (isset($this->request->get['manufacturer_id'])) {
				$this->request->get['route'] = 'product/manufacturer';
			} elseif (isset($this->request->get['information_id'])) {
				$this->request->get['route'] = 'information/information';
			}
			
			if (isset($this->request->get['route'])) {
				return $this->forward($this->request->get['route']);
			}
		}
	}
	
	// Passed value such as "51" or "35_36_51" - with the upper most category being first
	public function VerifyCategoryPath($catIds){
		// if its only one level deep
		if(strpos($catIds,"_") == false){
			return $this->CheckCategoryParent($catIds, 0);
		}
		
		$parts = explode('_', $catIds);

		$prevParentId = 0;
		foreach ($parts as $part) {
			if($this->CheckCategoryParent($part, $prevParentId) == false){
				return false;
			}
			$prevParentId = $part;
		}	

		return true;
	}

	public function CheckCategoryParent($catId, $parentid){
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category WHERE parent_id = '" . $parentid . "' AND category_id = '".$catId."'");
			if ($query->num_rows) {
				return true;
			}
			return false;
	}
}
?>

Overview

After line 33:

Code: Select all

$this->request->get['route'] = 'error/not_found';
add:

Code: Select all

return;	// If ANY part of the URL fails, RETURN so we show a 404 otherwise, if the NEXT part of the url is valid, we 'skip' the 404! Bad.
Change line 42:

Code: Select all

} elseif (isset($this->request->get['path'])) {
			$this->request->get['route'] = 'product/category';
To this:

Code: Select all

} elseif (isset($this->request->get['path'])) {
				if($this->VerifyCategoryPath($this->request->get['path']) == true){
					$this->request->get['route'] = 'product/category';
				}
				else{
					$this->request->get['route'] = 'error/not_found';	
				}

And at the bottom of the file, just before:

Code: Select all

}
?>
Put this:

Code: Select all

	// Passed value such as "51" or "35_36_51" - with the upper most category being first
	public function VerifyCategoryPath($catIds){
		// if its only one level deep
		if(strpos($catIds,"_") == false){
			return $this->CheckCategoryParent($catIds, 0);
		}
		
		$parts = explode('_', $catIds);

		$prevParentId = 0;
		foreach ($parts as $part) {
			if($this->CheckCategoryParent($part, $prevParentId) == false){
				return false;
			}
			$prevParentId = $part;
		}	

		return true;
	}

	public function CheckCategoryParent($catId, $parentid){
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category WHERE parent_id = '" . $parentid . "' AND category_id = '".$catId."'");
			if ($query->num_rows) {
				return true;
			}
			return false;
	}

Seems to work fine. I've not had chance to test it fully yet as its a bit late here...

Attachments

1.4.9.3 Seo Url - Modified


LoveMoissanite.com - Moissanite Rings - Proudly Powered by Opencart
[How To] Speed Up Page Content with Opencart - Opencart advocate since 2009


New member

Posts

Joined
Fri Sep 11, 2009 8:12 pm

Post by Skyhigh » Sun May 15, 2011 5:40 pm

I've given it a good test and it seems to work fine, even when you're just requesting a 'category only' url such as:
http://www.blahblah.com/cat1invalid
http://www.blahblah.com/cat1/cat2invalid/cat3
http://www.blahblah.com/cat1/cat2/cat3/ ... at5invalid[/b]

and also if you request an invalid category which forms part of a product url, such as:
http://www.blahblah.com/cat1/cat2invalid/my-valid-product-here
http://www.blahblah.com/cat1/cat2/cat3invalid/cat4/my-valid-product-here

Anyone else found any issues with this modification to SEO URL yet?

LoveMoissanite.com - Moissanite Rings - Proudly Powered by Opencart
[How To] Speed Up Page Content with Opencart - Opencart advocate since 2009


New member

Posts

Joined
Fri Sep 11, 2009 8:12 pm
Who is online

Users browsing this forum: No registered users and 331 guests