Page 1 of 1

SEO url for lighty and other

Posted: Mon May 17, 2010 3:15 am
by n[oO]ne
Hi,

i'm running OpenCart on a lighttpd webserver. OpenCart only supports Apache and his htaccess method thats why i tried to change the seo_url in OpenCart to run on lighty also.

Why i'm doing this?

1. Problem
Lighty doesn't support rewrite checks on files or directories (files yes but not directories)
2. Problem
You need to add a seo url for every product or category. If you have many categories or products it is not doable


My Target

Using Category- and Productnames to generate SEO Urls from it. Reserved chars should be replaced. Additional database request should be avoided

Current situation

This is my rewrite for lighttpd

Code: Select all

$HTTP["host"] =~ "^(www\.)?myhost.com" {
	url.rewrite-once = (
		# Only for categories
		".*,pc([0-9_]+)\.php$" => "/index.php?route=product/category&path=$1",
		# Only for products
		".*,c([0-9_]+),pp([0-9]+)\.php$" => "/index.php?route=product/product&path=$1&product_id=$2"
	)
}
Some of you maybe see that its unimportant what the links contains, essential is that the path and/or the product id is appended

For example we have a product with the product id 12 and a path 12_43_630:
BIDIA© graphicscard version with 512MB, Shader 3 Model and Direct-X® 10!

We generate something like that

Code: Select all

BIDIA-graphicscard-version-with-512MB-Shader-3-Model-and-Direct-X-10,c12_43_630,pp12.php
Or we have a category like this: Graphicscards(12) -> BIDIA Chip(43) -> 512MB(630)
We generate something like this:

Code: Select all

Graphicscard-BIDIA-Chip-512MB,pc12_43_630.php
Here the code from opencart/catalog/model/tool/seo_url.php

Code: Select all

$replace_from = array('?', '=', ' ', '/', '&', 'ä', 'Ä', 'ö', 'Ö', 'ü', 'Ü', 'ß', '>', '<', '"', '\'', '©', '®', '.', '+', ')', '(', '[', ']', '$',',');
$replace_to = array('', '', '-', '-', '-', 'ae', 'Ae', 'oe', 'Oe', 'ue', 'Ue', 'sz', '-', '-', '', '', '', '', '', '', '', '', '', '','','');

if( isset($data['path']) && isset($data['product_id']) ) {
    $sql  = 'SELECT name FROM ' . DB_PREFIX . 'product_description ';
    $sql .= 'WHERE product_id="' (int)$data['product_id'] . '" LIMIT 1';
    $query = $this->db->query( $sql );
    if( $query->num_rows ) {
        $name = htmlspecialchars_decode($query->row['name']);
        $name = str_replace($replace_from, $replace_to, $name );
        $url = '/' . $name;
    }
    $url .= ',c' . $data['path'] . ',pp' . $data['product_id'] .'.php';
    $url = preg_replace( '/(\-{2,})/', '-', $url );
    unset($data['product_id'], $data['path']);
}
elseif( isset($data['path']) && !isset($data['product_id']) ){
    $categories = explode( '_', $data['path'] );

    foreach( $categories as $category ) {
        $sql  = 'SELECT name FROM ' . DB_PREFIX . 'category_description ';
        $sql .= 'WHERE category_id="' . (int)$category . '" LIMIT 1';
        $query = $this->db->query( $sql );

        if( $query->num_rows ) {
            $name = $query->row['name'];
            $name = str_replace($replace_from, $replace_to, $name );
            $url .= ( $url == '' ) ? '/' . $name: '-' . $name;
        }
    }
    $url .= ',pc' . $data['path'] .'.php';
    $url = preg_replace( '/(\-{2,})/', '-', $url );
    unset($data['path']);
}

foreach ($data as $key => $value) {
    if (($key == 'manufacturer_id') || ($key == 'information_id')) {
        $sql  = 'SELECT * FROM ' . DB_PREFIX . 'url_alias WHERE ';
        $sql .= $this->db->escape($key . '=' . (int)$value);
        $query = $this->db->query();

        if ($query->num_rows) {
            $url .= '/' . $query->row['keyword'];

            unset($data[$key]);
        }
        
    }
}  
The code is not yet optimized or something. In the replace arrays some more chars needed also. Its mainly done for the german market

What else must be done

Caching! Something like collecting only changed or added products or categories.

What i want :)

What do you think about this method? Also i'm looking for some tipps about caching and the problem with the replace stuff. Would it be a good idea to add language files where people can add specific chars?

Thanks for the help and hopefully constructive advice

Disclaimer
Sorry for my bad english ^^

Re: SEO url for lighty and other

Posted: Wed Jul 07, 2010 4:45 pm
by kon
Works for me:

Code: Select all

url.rewrite-once = ( "^/$" => "/index.php?route=common/home" )
url.rewrite-if-not-file = ( "^/(.*)" => "/index.php?_route_=$1" )

Re: SEO url for lighty and other

Posted: Thu Jul 08, 2010 1:08 am
by n[oO]ne
Yes this works, but only with lighttpd 1.4.24 and up.
Debian stable for example is 1.4.19-5+lenny1, and you still have the problem to add rewrite urls to every single product.

My Code above, generates Urls from the product name, so i don't need to add it to every product by hand

Re: SEO url for lighty and other

Posted: Thu Jul 08, 2010 3:01 pm
by kon
I love Debian too.
But you can backport 1.4.24 to Lenny, or simple rebuild 1.4.19-5+lenny1 with patch from here:
http://redmine.lighttpd.net/projects/li ... sions/2647

Next, i write small ugly perl script for generate Urls.
Look here: http://www.afkt.ru/opencartdata/psevdonim.pl
Russian language only.
I prefer not to interfere in the work of opencart kernel. I only manipulate data in the database.

Re: SEO url for lighty and other

Posted: Wed Jan 04, 2012 9:45 pm
by CyberCr33p
Check these LUA rewrites for Lighttpd:

http://www.cretaforce.gr/blog/673-light ... s-opencart

Re: SEO url for lighty and other

Posted: Sat Jan 07, 2012 4:13 am
by darknight
Good post. I appreciate it! Thanks for guiding