Advertisement:

Author Topic: Redirect to category when item is deleted  (Read 194 times)

vivekshan

  • Newbie
  • *
  • Posts: 25
Redirect to category when item is deleted
« on: January 09, 2019, 11:48:24 pm »
I had seen some post to redirect deleted item pages to categories

as

<?php
function cust_redirect_on_item_not_exists($item) {
    if( count($item) == 0 ) {
        $category = Category::newInstance()->findBySlug(preg_replace('~^.*/(.*)/.*$~', '$1', $_SERVER['REQUEST_URI']));

        Session::newInstance()->_dropMessage('pubMessages'); // Prevent messages to be displayed multiple times
        osc_add_flash_info_message(__('Sorry, the page you are looking for does not exist.','bender'));
        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');
?>

but i had a problem and that is

i am using subdomain feature

and want to get redirected deleted items to categories only to base url without subdomain

can it be possible

vivekshan

  • Newbie
  • *
  • Posts: 25
Re: Redirect to category when item is deleted
« Reply #1 on: January 12, 2019, 10:49:49 pm »
i know that there are some useless fellow on this forum who help to resolve issue but some useful people are out of coverage area.

help me those people who feel that they are useful to others. :'(

patrickFromCroatia

  • Hero Member
  • *****
  • Posts: 541
  • Native Wordpress Widget for Osclass - coming soon
Re: Redirect to category when item is deleted
« Reply #2 on: January 12, 2019, 11:22:59 pm »
Hello,

Can you post a link to your site to check something? I never used subdomains feature before.

Regards.

vivekshan

  • Newbie
  • *
  • Posts: 25
Re: Redirect to category when item is deleted
« Reply #3 on: January 13, 2019, 11:06:05 pm »
Hello,

Can you post a link to your site to check something? I never used subdomains feature before.

Regards.

i dont want to post my website name here

so i have mentioned the code before and my problem is

before deleting items:-
if there is single item/ad in pune area then the same ad is in www.example.com/jobs/xxxxxxxx_i11580 and in pune.example.com/jobs/xxxxxxxx_i11580

if google has opted to show/include link as pune.example.com/jobs/xxxxxxxx_i11580 to users in search results then after

if item is deleted, users clicking on that link will be redirected to pune.example.com/jobs

where there is no ads available pune.example.com/jobs

but there are many relevant ads available in www.example.com/jobs

i want to redirect to categories without subdomain as base url and also i dont want to remove subdomain feature from my site

please help on this
 

patrickFromCroatia

  • Hero Member
  • *****
  • Posts: 541
  • Native Wordpress Widget for Osclass - coming soon
Re: Redirect to category when item is deleted
« Reply #4 on: January 14, 2019, 10:14:45 pm »
Okay, try using this.

Code: [Select]
<?php
function wm_item_404_redirect($item) {
    if(
count($item) == 0  {
        
$category Category::newInstance()->findBySlug(preg_replace('~^.*/(.*)/.*$~''$1'$_SERVER['REQUEST_URI'])); // Get category from URL slug.

        
Session::newInstance()->_dropMessage('pubMessages'); // Prevent messages to be displayed multiple times.
        
osc_add_flash_info_message(__('Sorry, the page you are looking for does not exist.','bender')); // Show error.
        
$url osc_search_url(array('sCategory' => $category['pk_i_id'])); // Get URL.
        
$url strstr($url'.'); // Remove subdomain part.
        
$url substr($url1); // Remove "." from subdomain part.
        
if($ret parse_url($url)) { // Add // prefix.
            
if (!isset($ret["scheme"]))
                
$url '//'.$url;
        }
        
osc_redirect_to($url301); // Redirect.
        
exit; 
    }
    
    return 
$item;
}
osc_add_filter('pre_show_item''wm_item_404_redirect');
?>


Regards.
« Last Edit: January 14, 2019, 10:16:28 pm by patrickFromCroatia »

vivekshan

  • Newbie
  • *
  • Posts: 25
Re: Redirect to category when item is deleted
« Reply #5 on: January 15, 2019, 09:52:02 pm »
Okay, try using this.

Code: [Select]
<?php
function wm_item_404_redirect($item) {
    if(
count($item) == 0  {
        
$category Category::newInstance()->findBySlug(preg_replace('~^.*/(.*)/.*$~''$1'$_SERVER['REQUEST_URI'])); // Get category from URL slug.

        
Session::newInstance()->_dropMessage('pubMessages'); // Prevent messages to be displayed multiple times.
        
osc_add_flash_info_message(__('Sorry, the page you are looking for does not exist.','bender')); // Show error.
        
$url osc_search_url(array('sCategory' => $category['pk_i_id'])); // Get URL.
        
$url strstr($url'.'); // Remove subdomain part.
        
$url substr($url1); // Remove "." from subdomain part.
        
if($ret parse_url($url)) { // Add // prefix.
            
if (!isset($ret["scheme"]))
                
$url '//'.$url;
        }
        
osc_redirect_to($url301); // Redirect.
        
exit; 
    }
    
    return 
$item;
}
osc_add_filter('pre_show_item''wm_item_404_redirect');
?>


Regards.

Thanks for your reply but its throwing 500 error
any help

patrickFromCroatia

  • Hero Member
  • *****
  • Posts: 541
  • Native Wordpress Widget for Osclass - coming soon
Re: Redirect to category when item is deleted
« Reply #6 on: January 16, 2019, 08:42:45 am »
Oops, a missing bracket.
Try this then:

Code: [Select]
<?php
function wm_item_404_redirect($item) {
    if(
count($item) == 0)  {
        
$category Category::newInstance()->findBySlug(preg_replace('~^.*/(.*)/.*$~''$1'$_SERVER['REQUEST_URI'])); // Get category from URL slug.

        
Session::newInstance()->_dropMessage('pubMessages'); // Prevent messages to be displayed multiple times.
        
osc_add_flash_info_message(__('Sorry, the page you are looking for does not exist.','bender')); // Show error.
        
$url osc_search_url(array('sCategory' => $category['pk_i_id'])); // Get URL.
        
$url strstr($url'.'); // Remove subdomain part.
        
$url substr($url1); // Remove "." from subdomain part.
        
if($ret parse_url($url)) { // Add // prefix.
            
if (!isset($ret["scheme"]))
                
$url '//'.$url;
        }
        
osc_redirect_to($url301); // Redirect.
        
exit; 
    }
    
    return 
$item;
}
osc_add_filter('pre_show_item''wm_item_404_redirect');
?>


Regards.

vivekshan

  • Newbie
  • *
  • Posts: 25
Re: Redirect to category when item is deleted
« Reply #7 on: January 16, 2019, 08:56:04 pm »
Oops, a missing bracket.
Try this then:

Code: [Select]
<?php
function wm_item_404_redirect($item) {
    if(
count($item) == 0)  {
        
$category Category::newInstance()->findBySlug(preg_replace('~^.*/(.*)/.*$~''$1'$_SERVER['REQUEST_URI'])); // Get category from URL slug.

        
Session::newInstance()->_dropMessage('pubMessages'); // Prevent messages to be displayed multiple times.
        
osc_add_flash_info_message(__('Sorry, the page you are looking for does not exist.','bender')); // Show error.
        
$url osc_search_url(array('sCategory' => $category['pk_i_id'])); // Get URL.
        
$url strstr($url'.'); // Remove subdomain part.
        
$url substr($url1); // Remove "." from subdomain part.
        
if($ret parse_url($url)) { // Add // prefix.
            
if (!isset($ret["scheme"]))
                
$url '//'.$url;
        }
        
osc_redirect_to($url301); // Redirect.
        
exit; 
    }
    
    return 
$item;
}
osc_add_filter('pre_show_item''wm_item_404_redirect');
?>


Regards.

this code is also not working

as my website is example.in

and after your code my deleted ads are redirected to like

http://in/services/other-services

where example. is missing in the above url

patrickFromCroatia

  • Hero Member
  • *****
  • Posts: 541
  • Native Wordpress Widget for Osclass - coming soon
Re: Redirect to category when item is deleted
« Reply #8 on: January 16, 2019, 09:35:49 pm »
Can you post again an example URL without the code? The code should remove the first part of the URL before the dot so "test.example.com" should become "example.com".

Regards.