*

webcity

  • *****
  • 255 posts
301 redirect for expired ads
« on: January 09, 2023, 06:42:07 AM »
Hi,

Can someone please recommend how to code a 301 redirect for expired ads, to redirect to the category the expired ad was listed in?

My site uses the format: sitename.com/category/subcategory/listing-title_i12345 and I'm using the Epsilon theme.

For example if I go to : https://epsilon.mb-themes.com/for-sale/cameras-camera-accessories/canon-pix-z9_i773
I would like to receive a 301 redirect to: https://epsilon.mb-themes.com/for-sale/cameras-camera-accessories

A lot of the most popular classified sites use this method to gain extra traffic. If you show a 404 page, the visitor usually doesn't continue browsing.
Example of other sites:
https://www.freeads.co.uk/uk/buy-sell/pets/dogs/bulldog/383258031/boys-only-kc-registered-english-bulldog-pups/view
https://www.gumtree.com/p/volkswagen/dec-2010-volkswagen-beetle-1.6-luna-41000-miles-service-history-moted-1-4-2023-excellent-condition-/14487351112
https://www.vivastreet.co.uk/rent/west-bromwich-b70/-b-c-h--1-bed-studio-high-st--west-bromwich/3097478769

Many thanks
« Last Edit: January 09, 2023, 06:47:59 AM by webcity »

*

MB Themes

Re: 301 redirect for expired ads
« Reply #1 on: January 09, 2023, 03:27:51 PM »
@webcity
Code: [Select]
if(osc_item_is_expired()) {
  header('Location:' . osc_search_url(array('sCategory' => osc_item_category_id())), true, 301);
  exit;
  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots

*

webcity

  • *****
  • 255 posts
Re: 301 redirect for expired ads
« Reply #2 on: January 09, 2023, 04:30:23 PM »
Thanks for your help :-)

Where is the best place to to include the code?

Thanks again

*

MB Themes

Re: 301 redirect for expired ads
« Reply #3 on: January 09, 2023, 09:08:52 PM »
You can out it at too of item.php
  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots

*

webcity

  • *****
  • 255 posts
Re: 301 redirect for expired ads
« Reply #4 on: January 10, 2023, 03:27:32 AM »
Thanks, that works great :-)

Can you please suggest code to show when item has been deleted too. So when you go to the deleted item page it sends a 301 to the category the item was in?

Many thanks

*

MB Themes

Re: 301 redirect for expired ads
« Reply #5 on: January 10, 2023, 11:09:30 AM »
@webcity
That's not possible because not found item goes properly to 404 from core.
Maybe if you do this in init hook before everything else....
  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots

*

Ajit Sahane

  • ****
  • 178 posts
  • https://bestclassifiedsusa.com
Re: 301 redirect for expired ads
« Reply #6 on: February 17, 2023, 06:39:28 PM »
I am used this code for achieve this in past. Also tested latest version, try it. Its worked well.

add this code end of the theme function.php file

Code: [Select]
<?php
function cust_redirect_on_item_not_exists($item) {
    if( 
count($item) == ) {
        
$category Category::newInstance()->findBySlug(preg_replace('~^.*/(.*)/.*$~''$1'$_SERVER['REQUEST_URI']));
        
osc_redirect_to(osc_search_url(array('sCategory' => $category['pk_i_id'])), 301);
        exit; 
    }
    
    return 
$item;
}

osc_add_filter('pre_show_item''cust_redirect_on_item_not_exists');
?>
« Last Edit: February 24, 2023, 09:20:59 PM by MB Themes »

*

webcity

  • *****
  • 255 posts
Re: 301 redirect for expired ads
« Reply #7 on: February 18, 2023, 11:38:00 AM »
Hi Ajit,

Thanks for your help.

I can see that the code works great on your site.

When I use the code it doesn't redirect and I get the error message:
The listing hasn't been enabled. Please enable it in order to make it public

I think it maybe because I'm using the Item Validation Plugin on my site.

Can anyone suggest a solution?

It would be great if the code could be built-in to the core Osclass :-)


*

MB Themes

Re: 301 redirect for expired ads
« Reply #8 on: February 24, 2023, 09:22:03 PM »
Maybe you need to use init hook instead.
  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots