Page 1 of 1

0.7.7 URL Alias/SEO Friendly URLs

Posted: Wed Dec 12, 2007 7:48 am
by gv_ron
First off I'd like to say how clean and well organized the OpenCart code is.  A couple of years ago I worked with osCommerce and the difference between them is like night and day.

Please note that these changes were applied to v0.7.7.

I have been searching through the forums to see how to get URL aliases/SEO friendly URLs to work.  I got some hints, but no step-by-step how-to.  I've finally gotten SEO friendly URLs to work on my setup and would like to share what I did.

There is no requirement for the URL alias feature to be enabled.

I was unable to see how OpenCart could convert pretty URLs into something OpenCart could understand, so I added this code (four new lines) to library/application/router.php.

Code: Select all

if (isset($this->routes[$path[0]])) { /* existing code */
/* ... existing code ... */
} /* existing code */
else if(isset($path[1])) { /* new code */
    for ($i = 0; $i < sizeof($path); $i += 2) /* new code */
    $request->set($path[$i], isset($path[$i+1]) ? $path[$i+1] : NULL); /* new code */
} /* new code */
After adding this code you can test it by navigating to a page and substituting [forward] slashes for the equals, ampersand and question mark characters in your browser's address bar.  If it is working you will remain on the same page when you click 'Go'.

Next some work needs to be done in library/environment/url.php.

Rename the last function in the file from 'create' to 'create_ugly' and add the following replacement function wherever you wish (in url.php).

Code: Select all

	
	function create($server, $controller, $action = NULL, $query = array()) {
		if(strpos($server, "/admin/") > 0) {
			return $this->create_ugly($server, $controller, $action, $query);
		}

		$link = 'controller/' . $controller;
		
		if ($action) {
			$link .= '/action/' . $action;
		}
		
		foreach ($query as $key => $value) {
			if ($value) {
				$link .= '/' . $key . '/' . urlencode($value);
			}
		}
		
		if (isset($this->data[$link])) {
			$link = $this->data[$link];
		} else {
			$link = 'index.php/' . $link;
		}
		
		return $server . $link;
	}
The above function was posted somewhere on the forum, however the first three lines I added.

URLs in the catalog should now be SEO friendly, but administration URLs will be unchanged.

That's it.

Though it isn't open for business, this is the site I'm bringing up (with the modifications described in this post).

http://www.eeegads.com

Re: 0.7.7 URL Alias/SEO Friendly URLs

Posted: Wed Dec 12, 2007 5:21 pm
by bo-oz
Wow... thanks a million for posting this fix!!

I allready read your earlier post about this problem and was also looking for a solution.

Only thing is: Your new if{}else{} structure, where should this be placed in the router.php? i.e. in what method? constuct/setroute/route? Or all of them?

my bad... I need to learn to read :P

Re: 0.7.7 URL Alias/SEO Friendly URLs

Posted: Wed Dec 12, 2007 5:25 pm
by bo-oz
Another one, I  commented out the following line:

Code: Select all

	function create($server, $controller, $action = NULL, $query = array()) {
		if(strpos($server, "/admin/") > 0) {
			return $this->create_ugly($server, $controller, $action, $query);
		}

		$link = 'controller/' . $controller;
		
		if ($action) {
			$link .= '/action/' . $action;
		}
		
		foreach ($query as $key => $value) {
			if ($value) {
				$link .= '/' . $key . '/' . urlencode($value);
			}
		}
		
		if (isset($this->data[$link])) {
			$link = $this->data[$link];
		} else {
			//$link = 'index.php/' . $link;
		}
		
		return $server . $link;
	}
I did this because the url's contained index.php like this:

http://domain/shop/index.php/controller/cart

now they are like:

http://domain/shop/controller/cart

Any thoughts on this? Could this go wrong?

edit: Ofcourse in combination with an appropriate .htaccess

Re: 0.7.7 URL Alias/SEO Friendly URLs

Posted: Thu Dec 13, 2007 1:51 am
by gv_ron
That looks like a good change for what you want to do.  Your .htaccess rule would probably be similar to the following.

Code: Select all

RewriteRule ^shop/(.*) shop/index.php/$1 [L]
Also to clarify the original post, the additional four lines of code are added to the route function within library/application/router.php.

This topic is actually my first post.

Re: 0.7.7 URL Alias/SEO Friendly URLs

Posted: Thu Dec 20, 2007 3:41 am
by ronh
gv_ron

Awesome, contribution. Would it be possible for you to post the updated php files to this discussion for easier implementation?

-Ron

Re: 0.7.7 URL Alias/SEO Friendly URLs

Posted: Sat Jan 12, 2008 3:41 pm
by madaha
Thanks so much! I have been looking for solution of URL alias for a long time.
Possible to go further:

1.  The URL now is "http://www.domain.com/controller/product/product_id/123". I don't want to have the "controller" inside the URL. 

I tried to replace "$link = 'controller/' . $controller;" by "$link = '' . $controller;" in the url.php file. The "conroller/" part disappears in the URL. URL looks perfect for me "http://www.domain.com/product/product_id/123". But it cause a problem: the search function not work any more. Can this problem be sloved?

2. I want to replace the "product/product_id/123" in the URL by "product/model.htm". The "model" is the one which input on "data" tab when add a new product in admin.

Hope to see a solution for above two points.

Re: 0.7.7 URL Alias/SEO Friendly URLs

Posted: Sun May 11, 2008 6:03 pm
by jamble
Sorry for the bump on this topic ...

I've installed the files in Dave's zip above and it appears to work well but I'm finding that once I go to a page other than the homepage it's breaking the call to the css and images on the site. All the html and products are fine, just no images or css.

Anyone else encountered this and know how to fix?

I've just copied the two files in the zip over the existing ones and the htaccess is the standard one without modification. I assume I've missed something really simple but I'm not sure what?

Cheers,

J.

Re: 0.7.7 URL Alias/SEO Friendly URLs

Posted: Tue Jun 10, 2008 6:55 am
by mcphearson
Not working for me

im getting a message saying "No input file specified." on a blank page

by the way i uploaded dave's folder he put together