Table of Contents

Shorten & Prettify URLs

This mod will re-write your urls to look nice and pretty! There's several plugins that SEO your urls for you like this one but they all seemed pretty complicated so we wrote our own.

Go from this:

To this!

This mod will change some core code and can potentially break a lot of things. Do not use this mod if:

includes/functions/html_output.php

Find line 16:

function zen_href_link($page = '', $parameters = '', $connection = 'NONSSL', $add_session_id = true, $search_engine_safe = true, $static = false, $use_dir_ws_catalog = true) {

and replace with

function zen_href_link($page = '', $parameters = '', $connection = 'NONSSL', $add_session_id = true, $search_engine_safe = true, $static = false, $use_dir_ws_catalog = true, $try_short_urls = true) {

Tehn replace this code block (ln 43):

    if (!$static) {
      if (zen_not_null($parameters)) {
        $link .= 'index.php?main_page='. $page . "&" . zen_output_string($parameters);
      } else {
        $link .= 'index.php?main_page=' . $page;
      }
    } else {
      if (zen_not_null($parameters)) {
        $link .= $page . "?" . zen_output_string($parameters);
      } else {
        $link .= $page;
      }
    }

with

    $short_url_failed = true;
    if($try_short_urls == true && $page == 'product_info')
      {
        $sparam = explode('&', $parameters);
        $products_id_string = '';
        foreach($sparam as $full_parameter)
          {
            if( strstr($full_parameter, 'products_id=') !== FALSE)
		$products_id_string = str_replace('products_id=', '', $full_parameter);
          }
        if( $products_id_string != '')
          {
            $link = 'products/' . $products_id_string;
            $short_url_failed = false;
          }
      }
    elseif($try_short_urls == true && $page == 'index')
      {
        $sparam = explode('&', $parameters);
        $cat_id_string = '';
        foreach($sparam as $full_parameter)
          {
            if( strstr($full_parameter, 'cPath') !== FALSE)
              $cat_id_string = str_replace('cPath=', '', $full_parameter);
          }
        if($cat_id_string != '')
          {
            $link = 'category/' . $cat_id_string;
            $short_url_failed = false;
          }
      }
    elseif($try_short_urls == true && $page != '' && ($parameters == ''))
      {
	$page =	str_replace('index.php?main_page=', '' , $page);
        $link = str_replace('products_', '', $page) . '/';
        $short_url_failed = false;
      }
 
    if($short_url_failed){
      if (!$static) {
        if (zen_not_null($parameters)) {
          $link .= 'index.php?main_page='. $page . "&" . zen_output_string($parameters);
        } else {
          $link .= 'index.php?main_page=' . $page;
        }
      } else {
        if (zen_not_null($parameters)) {
          $link .= $page . "?" . zen_output_string($parameters);
        } else {
          $link .= $page;
        }
      }
    }

This will make the ubiquitous zen_href_link() function try to generate a good-looking url. If it fails, it will fall back on the old type.

This will change any links that are generated by this function, but you should go through your template files and make sure that there aren't hard-coded links to the old type of address also.

.htaccess

Before you begin, always make a backup of your .htaccess file (we're going to be editing the one i your root html directory).

#redirect old product urls
RewriteCond %{QUERY_STRING} ^main_page=product_info&(cPath=[0-9]+(_[0-9]+)?&)?products_id=([0-9]+)$
RewriteRule ^index.php$ /products/%3? [R=301]

#redirect old category urls
RewriteCond %{QUERY_STRING} ^main_page=index&cPath=([0-9]+)$
RewriteRule ^index.php$ /category/%1? [R=301]


#rewrite back to the old page
RewriteRule ^product[s]?/([0-9]+) index.php?hidden=yes&main_page=product_info&products_id=$1 [QSA,L]
RewriteRule ^(category|categories)/([0-9_]+) index.php?hidden=yes&main_page=index&cPath=$2 [QSA,L]

The first two blocks of this code redirects from the old, ugly type to the new, short type. The [R=301] flag will redirect the client browser, so the url will look nice in their address bar even if they somehow click on an ugly url.

The last two lines redirect BACK from the nice-looking url to the ugly one, so that zencart will parse it correctly and output the correct page. As you could probably tell, this is very fragile and should cause an infinite loop of redirecting - make sure not to forget the [L] flag on the last two lines, it will make sure that it's the last command that is processed.

If you'd like to change your define pages to have nice urls as well, you can add

RewriteRule ^MY_DEFINE_PAGE[/]?$ \?main_page=MY_DEFINE_PAGE

for each one, replacing MY_DEFINE_PAGE with the name of your define page.