PrestaShop Forum

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

PrestaShop Forum

Jump to content

 

Friendly urls without ID number?

88 replies to this topic
#1
krat

    PrestaShop Newbie

  • Members
  • Pip
  • 6 posts
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

#2
HA!*!*Y

    PrestaShop Apprentice

  • Members
  • PipPip
  • 158 posts
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.

If you need help with PS, I am willing to trade for help with blog, back link's. PM me.

#3
rysiu.h

    PrestaShop Newbie

  • Members
  • Pip
  • 1 posts
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

#4
HA!*!*Y

    PrestaShop Apprentice

  • Members
  • PipPip
  • 158 posts
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.

If you need help with PS, I am willing to trade for help with blog, back link's. PM me.

#5
PC-SERVEIS Diseño Web

    PrestaShop Newbie

  • Members
  • Pip
  • 3 posts
Thank You very much for Sharing your PrestaShop code ;)

Greetings from Barcelona !

#6
JessieMon

    PrestaShop Newbie

  • Members
  • Pip
  • 1 posts
Very helpful. Thanks!

#7
C.Perez

    PrestaShop Apprentice

  • Members
  • PipPip
  • 69 posts
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.

#8
Will_Authentic_Style

    PrestaShop Newbie

  • Members
  • Pip
  • 5 posts
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?

#9
HA!*!*Y

    PrestaShop Apprentice

  • Members
  • PipPip
  • 158 posts
FREE module available now:
http://www.prestasho...post__p__883380

View PostWill_Authentic_Style, on 14 June 2012 - 04:43 PM, said:

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.

If you need help with PS, I am willing to trade for help with blog, back link's. PM me.

#10
HA!*!*Y

    PrestaShop Apprentice

  • Members
  • PipPip
  • 158 posts
FREE module available now:
http://www.prestasho...post__p__883380

View PostC.Perez, on 06 June 2012 - 09:13 AM, said:

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.

If you need help with PS, I am willing to trade for help with blog, back link's. PM me.

#11
Will_Authentic_Style

    PrestaShop Newbie

  • Members
  • Pip
  • 5 posts

View PostHA!*!*Y, on 15 June 2012 - 07:58 AM, said:

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?

#12
emma_w

    PrestaShop Newbie

  • Members
  • Pip
  • 3 posts
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.


#13
HA!*!*Y

    PrestaShop Apprentice

  • Members
  • PipPip
  • 158 posts
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.

If you need help with PS, I am willing to trade for help with blog, back link's. PM me.

#14
emma_w

    PrestaShop Newbie

  • Members
  • Pip
  • 3 posts
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).

#15
corint1

    PrestaShop Apprentice

  • Members
  • PipPip
  • 171 posts
Don't work category in 1462 or 1482 ... what is wrong ?

(

01. Missing category ID )



#16
ODM

    PrestaShop Newbie

  • Members
  • Pip
  • 4 posts
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

#17
corint1

    PrestaShop Apprentice

  • Members
  • PipPip
  • 171 posts
working...:)... tks

#18
giuly

    PrestaShop Apprentice

  • Members
  • PipPip
  • 160 posts
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

#19
emma_w

    PrestaShop Newbie

  • Members
  • Pip
  • 3 posts
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?

#20
giuly

    PrestaShop Apprentice

  • Members
  • PipPip
  • 160 posts
sorry i write in wrong topic, my mistake, I would write on ean13 topic, sorry...