Vote to Improve PrestaShop

PrestaShop Forum: Build a free online shop. Count on the Community!

The best place in the world to ask questions about PrestaShop and get advice from our passionate community!

PrestaShop Forum: Build a free online shop. Count on the Community!

Jump to content

 
Photo

Friendly urls without ID number?


  • Please log in to reply
93 replies to this topic
krat

    PrestaShop Newbie

  • Members
  • Pip
  • 6 posts

Posted 23 May 2011 - 12:52 PM

How can i get friendly urls without the ID dbase numbers in it?
http://webshop.start...ontent/about-us instead of http://webshop.start...tent/4-about-us

HA!*!*Y

    PrestaShop Apprentice

  • Members
  • PipPip
  • 206 posts

Posted 10 May 2012 - 08:39 AM

FREE module available now:
http://www.prestasho...post__p__883380

Here is what i got:

1st file: ./controllers/ProductController.php

update the function preProcess()
public function preProcess()
{
  if ($id_product = (int)Tools::getValue('id_product'))
   { $this->product = new Product($id_product, true, self::$cookie->id_lang); }
  else //remove link id by Ha!*!*y
  {
   $id_product = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
	SELECT `id_product`
	FROM `'._DB_PREFIX_.'product_lang`
	WHERE `link_rewrite` = \''.Tools::getValue('product_name_seo').'\'');
   $this->product = new Product($id_product, true, self::$cookie->id_lang);
  } //remove link id by Ha!*!*y
  if (!Validate::isLoadedObject($this->product))
  {
   header('HTTP/1.1 404 Not Found');
   header('Status: 404 Not Found');
  }
  else
   $this->canonicalRedirection();
  parent::preProcess();
}


2nd file: ./classes/Link.php

update the function getProductLink()
public function getProductLink($id_product, $alias = NULL, $category = NULL, $ean13 = NULL, $id_lang = NULL)
{
  global $cookie;
  if (is_object($id_product))
  {
   $link = '';
   if ($this->allow == 1)
   {
	$link .= (_PS_BASE_URL_.__PS_BASE_URI__.$this->getLangLink((int)$id_lang));

	if (isset($id_product->category) AND !empty($id_product->category) AND $id_product->category != 'home')
	 $link .= $id_product->category.'/';
	else
	 $link .= '';
	//remove link id by Ha!*!*y
	//$link .= (int)$id_product->id.'-';
	if (is_array($id_product->link_rewrite))
	 $link.= $id_product->link_rewrite[(int)$cookie->id_lang];
	else
	  $link.= $id_product->link_rewrite;
	if ($id_product->ean13)
	 $link .='-'.$id_product->ean13;
	else
	 $link .= '';
	$link .= '.html';
   }
   else
   {
	$link .= (_PS_BASE_URL_.__PS_BASE_URI__.'product.php?id_product='.(int)$id_product->id);
   }
   return $link;
  }

  else if ($alias)
  {
   $link = '';
   if ($this->allow == 1)
   {
	$link .= (_PS_BASE_URL_.__PS_BASE_URI__.$this->getLangLink((int)$id_lang));

	if ($category AND $category != 'home')
	 $link .= $category.'/';
	else
	  $link .= '';
	//remove link id by Ha!*!*y
	//$link .= (int)$id_product.'-'.$alias;
	$link .= $alias;

	if ($ean13)
	 $link .='-'.$ean13;
	else
	 $link .= '';

	$link .= '.html';
   }
   else
	$link .=(_PS_BASE_URL_.__PS_BASE_URI__.'product.php?id_product='.(int)$id_product);
   return $link;
  }

  else
   return _PS_BASE_URL_.__PS_BASE_URI__.'product.php?id_product='.(int)$id_product;
}

3rd file: ./classes/Tools.php

update the function generateHtaccess()

find inside the function
  if ($multilang)
  {
   $tab['RewriteRule']['content']['^([a-z]{2})/[a-zA-Z0-9-]*/([0-9]+)\-[a-zA-Z0-9-]*\.html'] = 'product.php?id_product=$2&isolang=$1 [QSA,L]';
   $tab['RewriteRule']['content']['^([a-z]{2})/([0-9]+)\-[a-zA-Z0-9-]*\.html'] = 'product.php?id_product=$2&isolang=$1 [QSA,L]';

and add the following lines:
//remove link id by Ha!*!*y
			$tab['RewriteEngine']['content_no_id'][] = 'RewriteCond %{REQUEST_FILENAME} !-f';
			$tab['RewriteEngine']['content_no_id'][] = 'RewriteCond %{REQUEST_FILENAME} !-d';
			$tab['RewriteEngine']['content_no_id'][] = 'RewriteRule ^([a-z]{2})/[a-zA-Z0-9-]*/([a-zA-Z0-9-]*)\.html product.php?product_name_seo=$2&isolang=$1 [QSA,L]';
			$tab['RewriteEngine']['content_no_id'][] = 'RewriteCond %{REQUEST_FILENAME} !-f';
			$tab['RewriteEngine']['content_no_id'][] = 'RewriteCond %{REQUEST_FILENAME} !-d';
			$tab['RewriteEngine']['content_no_id'][] = 'RewriteRule ^([a-z]{2})/([a-zA-Z0-9-]*)\.html product.php?product_name_seo=$2&isolang=$1 [QSA,L]';
//remove link id by Ha!*!*y

also look for inside the function:
  $tab['RewriteRule']['content']['^([0-9]+)\-[a-zA-Z0-9-]*\.html'] = 'product.php?id_product=$1 [QSA,L]';
  $tab['RewriteRule']['content']['^[a-zA-Z0-9-]*/([0-9]+)\-[a-zA-Z0-9-]*\.html'] = 'product.php?id_product=$1 [QSA,L]';


and than add the following lines:
//remove link id by Ha!*!*y
		$tab['RewriteEngine']['content_no_id'][] = 'RewriteCond %{REQUEST_FILENAME} !-f';
		$tab['RewriteEngine']['content_no_id'][] = 'RewriteCond %{REQUEST_FILENAME} !-d';
		$tab['RewriteEngine']['content_no_id'][] = 'RewriteRule ^([a-zA-Z0-9-]*)\.html product.php?product_name_seo=$1 [QSA,L]';
		$tab['RewriteEngine']['content_no_id'][] = 'RewriteCond %{REQUEST_FILENAME} !-f';
		$tab['RewriteEngine']['content_no_id'][] = 'RewriteCond %{REQUEST_FILENAME} !-d';
		$tab['RewriteEngine']['content_no_id'][] = 'RewriteRule ^[a-zA-Z0-9-]*/([a-zA-Z0-9-]*)\.html product.php?product_name_seo=$1 [QSA,L]';
//remove link id by Ha!*!*y

also look for inside the function:
  // Classic URL rewriting
  if ($rewrite_settings)
   foreach ($tab['RewriteRule']['content'] as $rule => $url)
	fwrite($writeFd, 'RewriteRule '.$rule.' '.__PS_BASE_URI__.$url."\n");


add this after:
//remove link id by Ha!*!*y
  if ($rewrite_settings){
   fwrite($writeFd, "\n");
   foreach ($tab['RewriteEngine']['content_no_id'] as $line => $rule){
	fwrite($writeFd, $rule."\n");
   }
  }
//remove link id by Ha!*!*y

Edited by HA!*!*Y, 06 August 2012 - 02:04 AM.


rysiu.h

    PrestaShop Newbie

  • Members
  • Pip
  • 1 posts

Posted 11 May 2012 - 02:47 PM

Hi HA!*!*Y

This solution has side effects:

All product metadata don't appearing and the title of page products show a default shop title.

You can fix this?, apart from this problem everything works fine

Thanks

Rysiu

HA!*!*Y

    PrestaShop Apprentice

  • Members
  • PipPip
  • 206 posts

Posted 14 May 2012 - 09:23 AM

FREE module available now:
http://www.prestasho...post__p__883380

i am look into it right now

this is a temporary solutions to the problem

Add this at the end of file: ./config/config.inc.php
  //remove link id by Ha!*!*y
  if (Tools::getValue('product_name_seo'))
  {
   $id_product = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
		SELECT `id_product`
		FROM `'._DB_PREFIX_.'product_lang`
		WHERE `link_rewrite` = \''.Tools::getValue('product_name_seo').'\'');
   $_GET['id_product'] = $id_product;
  }

Edited by HA!*!*Y, 06 August 2012 - 02:05 AM.


PC-SERVEIS Diseño Web

    PrestaShop Newbie

  • Members
  • Pip
  • 3 posts

Posted 21 May 2012 - 07:43 AM

Thank You very much for Sharing your PrestaShop code ;)

Greetings from Barcelona !

JessieMon

    PrestaShop Newbie

  • Members
  • Pip
  • 1 posts

Posted 03 June 2012 - 04:05 AM

Very helpful. Thanks!

C.Perez

    PrestaShop Apprentice

  • Members
  • PipPip
  • 69 posts

Posted 06 June 2012 - 09:13 AM

Works partially on my 1.4.5... It does remove the ID from products, but not from my categories and neither CMS links.. Any idea why ?? however it did the most important thing... remove the ID from products.. Thanks.

Will_Authentic_Style

    PrestaShop Newbie

  • Members
  • Pip
  • 5 posts

Posted 14 June 2012 - 04:43 PM

This also worked partially for me.

It removes the ID's from product URL's, but it doesn't update all my URL's automatically, so all my links to products are broken.

Do I need to do something to my htaccess file to do that?

HA!*!*Y

    PrestaShop Apprentice

  • Members
  • PipPip
  • 206 posts

Posted 15 June 2012 - 07:58 AM

FREE module available now:
http://www.prestasho...post__p__883380

This also worked partially for me.

It removes the ID's from product URL's, but it doesn't update all my URL's automatically, so all my links to products are broken.

Do I need to do something to my htaccess file to do that?


Yes Go to:
Back Office > Tools > Generators

Generate your ".htaccess" file by clicking the button

Edited by HA!*!*Y, 06 August 2012 - 02:05 AM.


HA!*!*Y

    PrestaShop Apprentice

  • Members
  • PipPip
  • 206 posts

Posted 15 June 2012 - 08:00 AM

FREE module available now:
http://www.prestasho...post__p__883380

Works partially on my 1.4.5... It does remove the ID from products, but not from my categories and neither CMS links.. Any idea why ?? however it did the most important thing... remove the ID from products.. Thanks.


C.Perez, i did not update the code to remove IDs from categories or CMS link ill try to post that some time

Edited by HA!*!*Y, 06 August 2012 - 02:06 AM.


Will_Authentic_Style

    PrestaShop Newbie

  • Members
  • Pip
  • 5 posts

Posted 15 June 2012 - 10:04 AM

Yes Go to:
Back Office > Tools > Generators

Generate your ".htaccess" file by clicking the button


Thanks for your reply HA!*!*Y.

I did try regenerating my htaccess file from the back-office, but it didn't work.

Could that be due to permissions on the file on the server? If I CHMOD the htaccess file to 777 will that work?

emma_w

    PrestaShop Newbie

  • Members
  • Pip
  • 3 posts

Posted 23 June 2012 - 05:50 PM

Is there any progress in getting this to work for categories and subcategories? I had a bash at it myself, using your amends as a basis but unfortunately my dev skills don't seem up to the job as it didn't work! I edited the Link.php, Tools.php and config file but wasn't sure if anything needed changing in the ProductController, so perhaps that's where the issue lies...

edit - Just realised it's the CategoryController file that needs editing, not ProductController of course. Unfortunately still not working so must be missing something?

Edited by emma_w, 23 June 2012 - 06:02 PM.


HA!*!*Y

    PrestaShop Apprentice

  • Members
  • PipPip
  • 206 posts

Posted 01 July 2012 - 10:19 PM

FREE module available now:
http://www.prestasho...post__p__883380


To remove number from Category URL

1st file: ./controllers/CategoryController.php

update the function preProcess()
	public function preProcess()
	{
		if ($id_category = (int)Tools::getValue('id_category'))
			{ $this->category = new Category($id_category, self::$cookie->id_lang); }
		else //remove link id by Ha!*!*y
		{
			$id_category = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
				SELECT `link_rewrite`
				FROM `'._DB_PREFIX_.'category_lang`
				WHERE `link_rewrite` = \''.Tools::getValue('category_name_seo').'\'');
			$this->category = new Category($id_category, self::$cookie->id_lang);
		} //remove link id by Ha!*!*y
....

2nd file: ./classes/Link.php

update the function getCategoryLink
I removed (int)($id_category->id).'-'. from the functions in 2 places

public function getCategoryLink($id_category, $alias = NULL, $id_lang = NULL)
{
  if (is_object($id_category))
   return ($this->allow == 1) ? (_PS_BASE_URL_.__PS_BASE_URI__.$this->getLangLink((int)($id_lang)).$id_category->link_rewrite) :
   (_PS_BASE_URL_.__PS_BASE_URI__.'category.php?id_category='.(int)($id_category->id));
  if ($alias)
   return ($this->allow == 1) ? (_PS_BASE_URL_.__PS_BASE_URI__.$this->getLangLink((int)($id_lang)).$alias) :
   (_PS_BASE_URL_.__PS_BASE_URI__.'category.php?id_category='.(int)($id_category));
  return _PS_BASE_URL_.__PS_BASE_URI__.'category.php?id_category='.(int)($id_category);
}

3rd file: ./classes/Tools.php

update the function generateHtaccess()

find inside the function
  if ($multilang)
  {
.....
....
	   	 $tab['RewriteRule']['content']['^([a-z]{2})/([0-9]+)\-[a-zA-Z0-9-]*(/[a-zA-Z0-9-]*)+'] = 'category.php?id_category=$2&isolang=$1&noredirect=1 [QSA,L]';
			$tab['RewriteRule']['content']['^([a-z]{2})/([0-9]+)\-[a-zA-Z0-9-]*'] = 'category.php?id_category=$2&isolang=$1 [QSA,L]';
....
....

Add the following lines:
//remove link id by Ha!*!*y
			$tab['RewriteEngine']['content_no_id'][] = 'RewriteCond %{REQUEST_FILENAME} !-f';
			$tab['RewriteEngine']['content_no_id'][] = 'RewriteCond %{REQUEST_FILENAME} !-d';
			$tab['RewriteEngine']['content_no_id'][] = 'RewriteRule ^([a-z]{2})/([a-zA-Z0-9-])(/[a-zA-Z0-9-]*)+ category.php?category_name_seo=$2&isolang=$1&noredirect=1 [QSA,L]';
			$tab['RewriteEngine']['content_no_id'][] = 'RewriteCond %{REQUEST_FILENAME} !-f';
			$tab['RewriteEngine']['content_no_id'][] = 'RewriteCond %{REQUEST_FILENAME} !-d';
			$tab['RewriteEngine']['content_no_id'][] = 'RewriteRule ^([a-z]{2})/([a-zA-Z0-9-]*) category.php?category_name_seo=$2&isolang=$1&noredirect=1 [QSA,L]';
//remove link id by Ha!*!*y

Find
  // Notice : the id_category rule has to be after product rules.
  // If not, category with number in their name will result a bug
  $tab['RewriteRule']['content']['^([0-9]+)\-[a-zA-Z0-9-]*(/[a-zA-Z0-9-]*)+'] = 'category.php?id_category=$1&noredirect=1 [QSA,L]';
  $tab['RewriteRule']['content']['^([0-9]+)\-[a-zA-Z0-9-]*'] = 'category.php?id_category=$1 [QSA,L]';

Add the following lines:
//remove link id by Ha!*!*y
			$tab['RewriteEngine']['content_no_id'][] = 'RewriteCond %{REQUEST_FILENAME} !-f';
			$tab['RewriteEngine']['content_no_id'][] = 'RewriteCond %{REQUEST_FILENAME} !-d';
			$tab['RewriteEngine']['content_no_id'][] = 'RewriteRule ^([a-zA-Z0-9-]*)(/[a-zA-Z0-9-]*) category.php?category_name_seo=$1&noredirect=1 [QSA,L]';
			$tab['RewriteEngine']['content_no_id'][] = 'RewriteCond %{REQUEST_FILENAME} !-f';
			$tab['RewriteEngine']['content_no_id'][] = 'RewriteCond %{REQUEST_FILENAME} !-d';
			$tab['RewriteEngine']['content_no_id'][] = 'RewriteRule ^([a-zA-Z0-9-]*) category.php?category_name_seo=$1 [QSA,L]';
//remove link id by Ha!*!*y

Add this at the end of file: ./config/config.inc.php
  //remove link id by Ha!*!*y
  if (Tools::getValue('product_name_seo'))
  {
   $id_product = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
			SELECT `id_product`
			FROM `'._DB_PREFIX_.'product_lang`
			WHERE `link_rewrite` = \''.Tools::getValue('product_name_seo').'\'');
   $_GET['id_product'] = $id_product;
  }
Than Go to:
Back Office > Tools > Generators

Generate your ".htaccess" file by clicking the button

Edited by HA!*!*Y, 06 August 2012 - 02:06 AM.


emma_w

    PrestaShop Newbie

  • Members
  • Pip
  • 3 posts

Posted 03 July 2012 - 10:07 AM

Brilliant, thanks for this. I will try this out as soon as I get the chance. I'm interested to see what bit of code I got wrong when trying it myself (probably the htaccess file - I must admit I don't quite understand all the rewrite code).

corint1

    PrestaShop Apprentice

  • Members
  • PipPip
  • 172 posts

Posted 03 July 2012 - 10:20 AM

Don't work category in 1462 or 1482 ... what is wrong ?

(

01. Missing category ID )



ODM

    PrestaShop Newbie

  • Members
  • Pip
  • 4 posts

Posted 04 July 2012 - 12:50 AM

if you find that there is an error with the category id
There is 1 error :
Missing category ID

you will have to do the fallowing

Change in /classes/Tools.php (just remove the "+" sign before the filename)
FROM

//remove link id by Ha!*!*y
$tab['RewriteEngine']['content_no_id'][] = 'RewriteCond %{REQUEST_FILENAME} !-f';
$tab['RewriteEngine']['content_no_id'][] = 'RewriteCond %{REQUEST_FILENAME} !-d';
$tab['RewriteEngine']['content_no_id'][] = 'RewriteRule ^([a-zA-Z0-9-]*)(/[a-zA-Z0-9-]*)+ category.php?category_name_seo=$1&noredirect=1 [QSA,L]';
$tab['RewriteEngine']['content_no_id'][] = 'RewriteCond %{REQUEST_FILENAME} !-f';
$tab['RewriteEngine']['content_no_id'][] = 'RewriteCond %{REQUEST_FILENAME} !-d';
$tab['RewriteEngine']['content_no_id'][] = 'RewriteRule ^([a-zA-Z0-9-]*)+ category.php?category_name_seo=$1 [QSA,L]';
//remove link id by Ha!*!*y
TO


//remove link id by Ha!*!*y
$tab['RewriteEngine']['content_no_id'][] = 'RewriteCond %{REQUEST_FILENAME} !-f';
$tab['RewriteEngine']['content_no_id'][] = 'RewriteCond %{REQUEST_FILENAME} !-d';
$tab['RewriteEngine']['content_no_id'][] = 'RewriteRule ^([a-zA-Z0-9-]*)(/[a-zA-Z0-9-]*) category.php?category_name_seo=$1&noredirect=1 [QSA,L]';
$tab['RewriteEngine']['content_no_id'][] = 'RewriteCond %{REQUEST_FILENAME} !-f';
$tab['RewriteEngine']['content_no_id'][] = 'RewriteCond %{REQUEST_FILENAME} !-d';
$tab['RewriteEngine']['content_no_id'][] = 'RewriteRule ^([a-zA-Z0-9-]*) category.php?category_name_seo=$1 [QSA,L]';
//remove link id by Ha!*!*y

Add at the end of /config/config.inc.php

if (Tools::getValue('category_name_seo'))
{
$id_category = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
SELECT `id_category`
FROM `'._DB_PREFIX_.'category_lang`
WHERE `link_rewrite` = \''.Tools::getValue('category_name_seo').'\'');
$_GET['id_category'] = $id_category;
}


and regenerate you htaccess

corint1

    PrestaShop Apprentice

  • Members
  • PipPip
  • 172 posts

Posted 04 July 2012 - 04:25 AM

working...:)... tks

giuly

    PrestaShop Apprentice

  • Members
  • PipPip
  • 160 posts

Posted 06 July 2012 - 11:05 AM

please I have 1.4.8.2 I tried to modify that file, but I do wrong some code, if I use file modified by me it doesn't work (500 error), please could you give to me file right for that version of ps?
Thanks

emma_w

    PrestaShop Newbie

  • Members
  • Pip
  • 3 posts

Posted 06 July 2012 - 11:40 AM

Have you modified ALL the files necessary?

./controllers/ProductController.php
./controllers/CategoryController.php
./classes/Link.php
./classes/Tools.php
./config/config.inc.php

..AND generated a new htaccess file through your admin?

giuly

    PrestaShop Apprentice

  • Members
  • PipPip
  • 160 posts

Posted 06 July 2012 - 12:39 PM

sorry i write in wrong topic, my mistake, I would write on ean13 topic, sorry...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users