Zen Cart Logo
Forums / General Questions / I've deleted products. How do I redirect customers to new ones? or to search instead?

I've deleted products. How do I redirect customers to new ones? or to search instead?

Views: 17,131

Results 1 to 20 of 38
17 Mar 2010, 6:18 AM
#1
jewelrywall avatar

jewelrywall

New Zenner

Join Date:
Mar 2010
Posts:
2
Plugin Contributions:
0

I've deleted products. How do I redirect customers to new ones? or to search instead?

Hi guys,
This is my question:
I have deleted many old watches products, and add many new watches product. ( i really should not do this)
Now have problems, some customers still can access old product page by google search.
Example: when custome access :
http://www.jewelrywall.com/rolex-oyster-perpetual-datejust-mens-watch-116233bksbrj-p-8638.html

the page will show: "Sorry, the product was not found. "

Now, i do not want the page show these words, i want the page redirect to search page, search keywords is :
rolex oyster perpetual datejust mens watch

then i edit the page (includes\templates\template_default\templates\tpl_product_info_noproduct.php), and add new codes:

//no product url: http://www.jewelrywall.com/rolex-oyster-perpetual-datejust-mens-watch-116233bksbrj-p-8638.html
function getUrl(){ 
$url="http://".$_SERVER["HTTP_HOST"]; 

if(isset($_SERVER["REQUEST_URI"])){ 
$url.=$_SERVER["REQUEST_URI"]; 
} 
else{ 
$url.=$_SERVER["PHP_SELF"]; 
if(!empty($_SERVER["QUERY_STRING"])){ 
$url.="?".$_SERVER["QUERY_STRING"]; 
} 
}
$eUrl=getUrl();
//exit("url=".$eUrl);
preg_match('/www\.jewelrywall\.com\/(.+)\-p\-([0-9]+)\.html/', $eUrl, $matches);
//print_r($matches);
$pname=str_replace('-','+',$matches[1]);
$searchUrl='http://www.jewelrywall.com/index.php?main_page=advanced_search_result&search_in_description=1&keyword='.$pname;
//print $pname;
zen_redirect($searchUrl);

But it do not work, how should i do?
Or have another better way to fix this "the product was not found" problem?

Thanks for helps!!!!!!!

Miles

17 Mar 2010, 11:32 AM
#2
mprough avatar

mprough

Totally Zenned

Join Date:
Nov 2007
Location:
Woodbine, Georgia, United States
Posts:
4,279
Plugin Contributions:
37

Re: I've deleted products. How do I redirect customers to new ones? or to search instead?

redirect 301 /rolex-oyster-perpetual-datejust-mens-watch-116233bksbrj-p-8638.html http://www.jewelrywall.com/

In your htaccess.

~Melanie

17 Mar 2010, 1:32 PM
#3
jewelrywall avatar

jewelrywall

New Zenner

Join Date:
Mar 2010
Posts:
2
Plugin Contributions:
0

Re: I've deleted products. How do I redirect customers to new ones? or to search instead?

Thanks ~Melanie!

But there are many other deleted products(about 1000), how use 301 in htaccess to redirect ?

i think the zencart system should kow the product is not existed, then redirect to other url, that is better, but i do not know how and where to modify?

20 Mar 2010, 7:49 PM
#4
7thveil avatar

7thveil

New Zenner

Join Date:
Feb 2007
Posts:
10
Plugin Contributions:
0

Re: I've deleted products. How do I redirect customers to new ones? or to search instead?

I'm here with a similar question. When a product gets deleted, the link to the deleted product generates the "no product" page. It's just a regular "product_info" page using the tpl_product_info_noproduct.php template.

Also—and this is important: the server responds with a 404 error! That should never happen for a product that's been deleted. The "no product" page shouldn't generate an error. Not sure why this happens.

So—how can we generate a more helpful response? The idea of returning search results is a very good one, but there's a lot of functionality missing (keyword lookup for one thing) that makes this not easily possible. How about just sending them to the category page? That should be possible, but by the time you're serving the template it's too late for a header redirect, which would be the most seamless and SEO-friendly way to do it.

I'm looking into a rewrite in the .htaccess that would send the visitor to the category page for the product.

20 Mar 2010, 8:50 PM
#5
7thveil avatar

7thveil

New Zenner

Join Date:
Feb 2007
Posts:
10
Plugin Contributions:
0

Re: I've deleted products. How do I redirect customers to new ones? or to search instead?

OK, here's the solution I'm using. Right up front I want to say this is not necessarily the best way to address this issue—partly because it alters a file that might be overwritten in an upgrade, and I would normally not do this because it leads to trouble down the road. (I tried using an override for this, but it didn't work)

So, if someone looks for a deleted product (because there's a link somewhere on the web that pointed to it) the user comes in with a URL like this: /index.php?main_page=product_info&cPath=4&products_id=61

I altered the file /includes/modules/pages/product_info/main_template_vars.php

On line 31 there's a check for a non-existent product. Normally it send the user to the "noproduct" page. For my change, I inserted a PHP header function like this:

$requeststring = (isset($_GET['cPath']))?'&cPath='. (int)$_GET['cPath']:'';
        header('location:index.php?main_page=index'.$requeststring);
```This sends them to the category the product was in. If the category is also non-existent, it goes to the "no products in this category" page, which is fine by me. If there's no category in the URL at all, it goes to the site index page.

Either way, no error is generated.

So this is what I did and so use this change at your own risk, test the hell out of it and use your own programming skills and common sense to do it better if you want.
20 Mar 2010, 9:23 PM
#6
mprough avatar

mprough

Totally Zenned

Join Date:
Nov 2007
Location:
Woodbine, Georgia, United States
Posts:
4,279
Plugin Contributions:
37

Re: I've deleted products. How do I redirect customers to new ones? or to search instead?

Also—and this is important: the server responds with a 404 error! That should never happen for a product that's been deleted. The "no product" page shouldn't generate an error. Not sure why this happens.
Because the page/product is gone and that or a permanent redirect is the proper response.

So—how can we generate a more helpful response? The idea of returning search results is a very good one, but there's a lot of functionality missing (keyword lookup for one thing) that makes this not easily possible. How about just sending them to the category page? That should be possible, but by the time you're serving the template it's too late for a header redirect, which would be the most seamless and SEO-friendly way to do it.

I'm looking into a rewrite in the .htaccess that would send the visitor to the category page for the product.

The page they are sent to is not an issue, sure you can add functionality here to assist the shoppers and I agree fully, but the correct response in this case is still a 404.

OK, here's the solution I'm using. Right up front I want to say this is not necessarily the best way to address this issue—partly because it alters a file that might be overwritten in an upgrade, and I would normally not do this because it leads to trouble down the road. (I tried using an override for this, but it didn't work)
Upgrade overwrites are the least of your worry, by removing the error you have created a new canonical address, probably with a 302 which is a serious duplication issue... especially wide scale like you are talking about.

Again, the resulting page is a non issue... send them where you wish. The server's response MUST be a 404 or a 301 or you are causing a great deal more than you are solving with duplication and redirect issues for non canonical urls.

~Melanie

20 Mar 2010, 10:30 PM
#7
7thveil avatar

7thveil

New Zenner

Join Date:
Feb 2007
Posts:
10
Plugin Contributions:
0

Re: I've deleted products. How do I redirect customers to new ones? or to search instead?

@mprough

Thanks for your informative response! I admit I'm somewhat ignorant of these issues with setting the proper error for a page that is not found. I guess I was thinking a "product not found" is not at all the same as a "page not found"

If I understand you correctly, I just need to add a 404 error response to my redirect.

thanks again

21 Mar 2010, 1:06 PM
#8
mprough avatar

mprough

Totally Zenned

Join Date:
Nov 2007
Location:
Woodbine, Georgia, United States
Posts:
4,279
Plugin Contributions:
37

Re: I've deleted products. How do I redirect customers to new ones? or to search instead?

7thVeil, I have edited your code and have some other relevant information and technique regarding this issue here http://pro-webs.net/blog/2010/03/21/deleted-out-of-stock-products/

I have always forced a permanent redirect by hand (ultimate control), but I certainly understand the need for a more large scale dynamic solution. So I edited your code to achieve this.

Cheers,

Melanie

22 Mar 2010, 5:31 PM
#9
7thveil avatar

7thveil

New Zenner

Join Date:
Feb 2007
Posts:
10
Plugin Contributions:
0

Re: I've deleted products. How do I redirect customers to new ones? or to search instead?

Nicely done— thanks!

1 Apr 2010, 1:18 PM
#10
dml73 avatar

dml73

Zen Follower

Join Date:
Oct 2007
Posts:
413
Plugin Contributions:
0

Re: I've deleted products. How do I redirect customers to new ones? or to search instead?

mprough:

7thVeil, I have edited your code and have some other relevant information and technique regarding this issue here http://pro-webs.net/blog/2010/03/21/deleted-out-of-stock-products/

I have always forced a permanent redirect by hand (ultimate control), but I certainly understand the need for a more large scale dynamic solution. So I edited your code to achieve this.

Cheers,

Melanie

Hi Melanie, the redirection example you gave:

redirect 301 /index.php?main_page=product_info&cPath=5&products_id=38 http://www.domain.com/

is not working on my site. I put it in the .htaccess file in the root but when I go to the product page it simply does not redirect. Do you have any idea why? I am not using any URL rewrite modules.

1 Apr 2010, 1:22 PM
#11
mprough avatar

mprough

Totally Zenned

Join Date:
Nov 2007
Location:
Woodbine, Georgia, United States
Posts:
4,279
Plugin Contributions:
37

Re: I've deleted products. How do I redirect customers to new ones? or to search instead?

It is because of your url rewrites. You can try putting the redirect after the Ultimate SEO rules, but likely it will leave you with an appended url for the resulting page.

This is one of the many reasons we no longer install urls rewrites.... The very small value does not outweigh the loss of functionality, options and load time lost.

~Melanie

1 Apr 2010, 1:36 PM
#12
g1smd avatar

g1smd

Zen Follower

Join Date:
Mar 2010
Location:
UK
Posts:
449
Plugin Contributions:
0

Re: I've deleted products. How do I redirect customers to new ones? or to search instead?

If you ever use, or intend to use, rewrites in the future (or if any of your other rules found in the .htaccess file use RewriteRule already) change the method slightly.

Instead of using Redirect or RedirectMatch, use RewriteRule with the [R=301,L] flags. It is important that all of your redirect/rewrite code belongs to the same Apache module.

To be clear, RewriteRule comes from Mod_Rewrite and Redirect and RedirectMatch come from Mod_Alias.

Execution order of your directives is controlled NOT by the order the code is listed in your .htaccess file, but by the module execution order within .htaccess.

That is, if Mod_Alias runs before Mod_Rewrite, your Redirect and RedirectMatch directives would be executed before your RewriteRule directives (good!) BUT if Mod_Rewrite runs before Mod_Alias, your RewriteRule directives would be executed before your Redirect and RedirectMatch directives (bad!).

The effect of that would be to expose rewritten filepaths back out onto the web as new URLs, to create unwanted redirection chains, and generally not work as expected.

You don't have control over the module execution order. Your host has set that up. Most get it right, but a few do not.

You can protect yourself from the problem now and in the future by using RewriteRule for all of your directives.

You can also take advantage of the very powerful pattern matching controls that RewriteRule has to offer. Be aware that RewriteRule sees only the 'path' part of the URL. If you need to examine the requested hostname or parameters or the original HTTP request itself, add a preceding RewriteCond to examine the required attribute.

In general, ```
RewriteRule ^some-pattern http://www.example.com/somepath [R=301,L]


If you use rewrites make sure that ALL of your redirects are listed before those.  This is because the redirects send the user to a new URL, and rewrites 'translate' a URL request into a different internal server filepath. Once rewrites have executed and are looking on the server's hard drive for the content it is way too late to be trying to alter the URL the user sees in the browser address bar.

Redirect first so that user sees the right URL. Once the user sees the correct URL, and only then, execute the rewrite to fetch the content.
1 Apr 2010, 1:45 PM
#13
g1smd avatar

g1smd

Zen Follower

Join Date:
Mar 2010
Location:
UK
Posts:
449
Plugin Contributions:
0

Re: I've deleted products. How do I redirect customers to new ones? or to search instead?

If you ever use, or intend to use, rewrites in the future (or if any of your other rules found in the .htaccess file use RewriteRule already) change the method slightly.

Instead of using Redirect or RedirectMatch, use RewriteRule with the [R=301,L] flags. It is important that all of your redirect/rewrite code belongs to the same Apache module.

To be clear, RewriteRule comes from Mod_Rewrite and Redirect and RedirectMatch come from Mod_Alias.

Execution order of your directives is controlled NOT by the order the code is listed in your .htaccess file, but by the module execution order within .htaccess.

That is, if Mod_Alias runs before Mod_Rewrite, your Redirect and RedirectMatch directives would be executed before your RewriteRule directives (good!) BUT if Mod_Rewrite runs before Mod_Alias, your RewriteRule directives would be executed before your Redirect and RedirectMatch directives (bad!).

The effect of that would be to expose rewritten filepaths back out onto the web as new URLs, to create unwanted redirection chains, and generally not work as expected.

You don't have control over the module execution order. Your host has set that up. Most get it right, but a few do not.

You can protect yourself from the problem now and in the future by using RewriteRule for all of your directives.

You can also take advantage of the very powerful pattern matching controls that RewriteRule has to offer. Be aware that RewriteRule sees only the 'path' part of the URL. If you need to examine the requested hostname or parameters or the original HTTP request itself, add a preceding RewriteCond to examine the required attribute.

In general, ```
RewriteRule ^some-pattern http://www.example.com/somepath [R=301,L]


If you use rewrites make sure that ALL of your external redirects are listed before the internal filepath rewrites.  This is because the redirects send the user to a new URL, and the rewrites 'translate' a URL request into a different internal server filepath. Once the rewrites have executed and are looking on the server's hard drive for the content, it is way too late to be trying to alter the URL the user sees in the browser address bar.

Redirect first so that user sees the right URL. Once the user sees the correct URL, and only then, execute the rewrite to fetch the content.
1 Apr 2010, 1:58 PM
#14
mprough avatar

mprough

Totally Zenned

Join Date:
Nov 2007
Location:
Woodbine, Georgia, United States
Posts:
4,279
Plugin Contributions:
37

Re: I've deleted products. How do I redirect customers to new ones? or to search instead?

g1smd:

If you ever use, or intend to use, rewrites in the future (or if any of your other rules found in the .htaccess file use RewriteRule already) change the method slightly.

Instead of using Redirect or RedirectMatch, use RewriteRule with the [R=301,L] flags. It is important that all of your redirect/rewrite code belongs to the same Apache module.

To be clear, RewriteRule comes from Mod_Rewrite and Redirect and RedirectMatch come from Mod_Alias.

Execution order of your directives is controlled NOT by the order the code is listed in your .htaccess file, but by the module execution order within .htaccess.

That is, if Mod_Alias runs before Mod_Rewrite, your Redirect and RedirectMatch directives would be executed before your RewriteRule directives (good!) BUT if Mod_Rewrite runs before Mod_Alias, your RewriteRule directives would be executed before your Redirect and RedirectMatch directives (bad!).

The effect of that would be to expose rewritten filepaths back out onto the web as new URLs, to create unwanted redirection chains, and generally not work as expected.

You don't have control over the module execution order. Your host has set that up. Most get it right, but a few do not.

You can protect yourself from the problem now and in the future by using RewriteRule for all of your directives.

You can also take advantage of the very powerful pattern matching controls that RewriteRule has to offer. Be aware that RewriteRule sees only the 'path' part of the URL. If you need to examine the requested hostname or parameters or the original HTTP request itself, add a preceding RewriteCond to examine the required attribute.

In general, ```
RewriteRule ^some-pattern http://www.example.com/somepath [R=301,L]

> 
> If you use rewrites make sure that ALL of your redirects are listed before those.  This is because the redirects send the user to a new URL, and rewrites 'translate' a URL request into a different internal server filepath. Once rewrites have executed and are looking on the server's hard drive for the content it is way too late to be trying to alter the URL the user sees in the browser address bar.
> 
> Redirect first so that user sees the right URL. Once the user sees the correct URL, and only then, execute the rewrite to fetch the content.

Unfortunately, even this method will produce a resulting url which is appended with a ? or ?string because of USEO. A good friend of mine in the UK and I worked very hard to accomplish this, and we did for a site which was changing platforms.... But trust me it was no easy task and surely not clean as we had to also strip the appended strings caused by USEO. It was many many lines of syntax as we could not grab a query string to batch any of them and sanitize them as well.

Best solution is to avoid SEO rewrites for your Zen Cart. Changing product titles changes and loses the indexed urls. Rewrites and redirects for anything are nearly impossible and the load speed is diminished somewhat as well.

If there were a cleaner solution, then sure rewrite them... But I have tested them all and the only one I think is close is Ceon. The rest all have some or all of the functionality issues above.

~Melanie
1 Apr 2010, 5:41 PM
#15
dml73 avatar

dml73

Zen Follower

Join Date:
Oct 2007
Posts:
413
Plugin Contributions:
0

Re: I've deleted products. How do I redirect customers to new ones? or to search instead?

mprough:

It is because of your url rewrites. You can try putting the redirect after the Ultimate SEO rules, but likely it will leave you with an appended url for the resulting page.

This is one of the many reasons we no longer install urls rewrites.... The very small value does not outweigh the loss of functionality, options and load time lost.

~Melanie

Im not using any of the URL rewrite modules and have never done. My .htaccess file looks like this:

<Files 403.shtml>
order allow,deny
allow from all
</Files>

redirects any URL that includes: record_company.php/password_forgotten.php this is a security patch

RedirectMatch Permanent ^/(.*[record_company.php]+)/(password_forgotten.php)$ /page_not_found.php

redirects any URL that includes: /images/wp- with 'wp-' being anything that ends with '.php'

this allows for images named such as 'wp-header.jpg' to work

RedirectMatch Permanent ^/(.[images]+)/(wp-..php)$ /page_not_found.php

redirect 301 /index.php?main_page=product_info&cPath=5&products_id=38 http://www.example.com/

ErrorDocument 404 /index.php?main_page=page_not_found

RewriteEngine On

Index Redirect for /index.(html?|php) with no parameters

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index.(html?|php)\ HTTP/
RewriteRule ^index.(html?|php)$ http://www.example.com/? [R=301,L]

Canonical Redirect non-www and/or appended port number to www [EXCLUDE /admin requests]

RewriteCond %{HTTP_HOST} !^(www.example.com)?$
RewriteCond %{REQUEST_URI} !^/admin [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

The redirect 301 /index.php?main_page=product_info&cPath=5&products_id=38 http://www.example.com/
does not redirect to http://www.example.com/ when I go to /index.php?main_page=product_info&cPath=5&products_id=38

Do you see anything Im doing wrong?

1 Apr 2010, 5:53 PM
#16
g1smd avatar

g1smd

Zen Follower

Join Date:
Mar 2010
Location:
UK
Posts:
449
Plugin Contributions:
0

Re: I've deleted products. How do I redirect customers to new ones? or to search instead?

Yes, there are a lot of problems. My earlier post (above) covers most of those. In summary.

Change all of the "Redirect" and "RedirectMatch" rules to use RewriteRule with [R=301,L] flags, and list those before all of your other rules.

You'll need to look at QUERY_STRING using a RewriteCond too.

The .* construct in your code is VERY inefficient. If it is meant to match none, one, or multiple folder levels, use something like (([^/]+)/*) instead. It will run hundreds of times faster. The new pattern means (("not a slash" one or more times), followed by slash) the whole of that "zero or more times").

[record_company.php] matches any single letter from the list r e c o r d _ m p a n y h and the + says "one or more times", so a request for "drocer_ynapmoc.hph" or for "cordpan" would also fulfill the pattern. The brackets aren't needed at all. You need to match the literal "record_company.php" not a list of letters.

1 Apr 2010, 6:02 PM
#17
g1smd avatar

g1smd

Zen Follower

Join Date:
Mar 2010
Location:
UK
Posts:
449
Plugin Contributions:
0

Re: I've deleted products. How do I redirect customers to new ones? or to search instead?

... and make sure you add the [L] flag to the end of every rule.

1 Apr 2010, 6:31 PM
#18
dml73 avatar

dml73

Zen Follower

Join Date:
Oct 2007
Posts:
413
Plugin Contributions:
0

Re: I've deleted products. How do I redirect customers to new ones? or to search instead?

g1smd:

Yes, there are a lot of problems. My earlier post (above) covers most of those. In summary.

Change all of the "Redirect" and "RedirectMatch" rules to use RewriteRule with [R=301,L] flags, and list those before all of your other rules.

You'll need to look at QUERY_STRING using a RewriteCond too.

The .* construct in your code is VERY inefficient. If it is meant to match none, one, or multiple folder levels, use something like (([^/]+)/*) instead. It will run hundreds of times faster. The new pattern means (("not a slash" one or more times), followed by slash) the whole of that "zero or more times").

[record_company.php] matches any single letter from the list r e c o r d _ m p a n y h and the + says "one or more times", so a request for "drocer_ynapmoc.hph" or for "cordpan" would also fulfill the pattern. The brackets aren't needed at all. You need to match the literal "record_company.php" not a list of letters.

Wow..thanks for the info, ... with my limited knowledge of .htaccess files I will need to re-read your comments a few times :D

1 Apr 2010, 6:44 PM
#19
g1smd avatar

g1smd

Zen Follower

Join Date:
Mar 2010
Location:
UK
Posts:
449
Plugin Contributions:
0

Re: I've deleted products. How do I redirect customers to new ones? or to search instead?

This is Server Configuration Code, so YOU need to know exactly how it works so that YOU can maintain it as your site evolves. Use the Mod_Rewrite documentation at apache.org for guidance as well as the examples here.

One simple typo can bring your whole site down (if you are lucky) or slowly and silently erode your search listings until the fault puts you and your site out of business. So, be sure you know what every line of code does and why it is there.

Make sure that every block of code has # comments explaining exactly what it does. You'll thank yourself six months later.

2 Apr 2010, 4:00 PM
#20
dml73 avatar

dml73

Zen Follower

Join Date:
Oct 2007
Posts:
413
Plugin Contributions:
0

Re: I've deleted products. How do I redirect customers to new ones? or to search instead?

Ok, to correct the RewriteRule I did the following:

RewriteEngine On

Index Redirect for /index.(html?|php) with no parameters

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index.(html?|php)\ HTTP/
RewriteRule ^index.(html?|php)$ http://www.example.com/? [R=301,L]

Canonical Redirect non-www and/or appended port number to www [EXCLUDE /admin requests]

RewriteCond %{HTTP_HOST} !^(www.example.com)?$
RewriteCond %{REQUEST_URI} !^/admin [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

#Redirects product page 187 to index
RewriteRule ^index.php?main_page=product_info&cPath=65&products_id=187 http://www.example.com/ [R=301,L]

But the redirect rule for product page 187 still isnt redirecting. What am I doing wrong?