Advertisement:

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

vivekshan

  • Jr. Member
  • **
  • Posts: 67
Redirect to category when item is deleted (solved)
« 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
« Last Edit: January 20, 2019, 09:48:58 pm by vivekshan »

vivekshan

  • Jr. Member
  • **
  • Posts: 67
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: 763
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

  • Jr. Member
  • **
  • Posts: 67
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: 763
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

  • Jr. Member
  • **
  • Posts: 67
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: 763
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

  • Jr. Member
  • **
  • Posts: 67
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: 763
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.

vivekshan

  • Jr. Member
  • **
  • Posts: 67
Re: Redirect to category when item is deleted
« Reply #9 on: January 19, 2019, 11:52:06 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

sorry for late reply

suppose the deleted url of and ad is present in google as http://pune.example.com/jobs/xxxxxxxx_i11580

when applying your function

its redirected to http://com/jobs

its good that pune subdomain is removed from the link but its bad that there is no domain

like example. is also missing in the url

http://example.com/jobs

example. is missing in the url

vivekshan

  • Jr. Member
  • **
  • Posts: 67
Re: Redirect to category when item is deleted
« Reply #10 on: January 20, 2019, 12:30:02 am »
if i am editing this line
$url = substr($url, 1); // Remove "." from subdomain part.

as

$url = substr($url, 1); // Remove " " from subdomain part.

then deleted ad http://pune.example.com/jobs/xxxxxxxx_i11580

is redirected to the correct path as

http://example.com/jobs/

but there is one problem and that is

if ad was in this link http://example.com/jobs/xxxxxxxx_i11580

then with my editing the url is not redirected on correct path

its been redirected to like this

http://com/jobs/

where there is no domain name present in the url with "." like (example.)

patrickFromCroatia

  • Hero Member
  • *****
  • Posts: 763
Re: Redirect to category when item is deleted
« Reply #11 on: January 20, 2019, 01:39:41 am »
Understood. Let me see... Enter the part of the site URL between the dots (eg. for www.google.com you put google) as value for variable $site.

I tested both examples ("http://pune.example.com/jobs/xxxxxxxx_i11580", "http://example.com/jobs/xxxxxxxx_i11580") and I got the proper URL each time.

Code: [Select]
<?php
function wm_item_404_redirect($item) {
    if(
count($item) == 0)  {
        
$site 'example'// Enter the part of the site URL[b] between the dot[/b]s (eg. for [i]www.gooogle.com[/i] you put [i]google[/i]).

        
$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.
        
$new_url strstr($url'.'); // Remove subdomain part.
        
$new_url substr($new_url1); // Remove "." from subdomain part.
        
if($ret parse_url($new_url)) { // Add // prefix.
            
if (!isset($ret["scheme"]))
                
$new_url '//'.$new_url;
        }
        if(\
strpos($new_url$site) !== false) { // Check if the new URL contains $site variable.
            
osc_redirect_to($new_url301); // It contains it - the URL is subdomain. Redirect to it.
        
} else {
            
osc_redirect_to($url301); // It doesn't contain it - the URL is not a subdomain. Redirect to default URL.
        
}
        exit; 
    }
    
    return 
$item;
}
osc_add_filter('pre_show_item''wm_item_404_redirect');
?>


Regards.
« Last Edit: January 20, 2019, 01:51:21 am by patrickFromCroatia »

vivekshan

  • Jr. Member
  • **
  • Posts: 67
Re: Redirect to category when item is deleted
« Reply #12 on: January 20, 2019, 08:50:05 pm »
its working
« Last Edit: January 20, 2019, 09:38:34 pm by vivekshan »

vivekshan

  • Jr. Member
  • **
  • Posts: 67
Re: Redirect to category when item is deleted (solved)
« Reply #13 on: January 20, 2019, 08:55:52 pm »
your code is working now

$site = 'example'; // Enter the part of the site URL between the dots (eg. for www.gooogle.com you put google).

i forgot to edit this line

$site = 'mydomain'; // Enter the part of the site URL between the dots (eg. for www.gooogle.com you put google).

thanks for it patrickFromCroatia for your excellent work

i thought that in this forum there are some people who solve others problem simply by copying code from core files

but now i am wrong, from your own code and effort it can be seen that you people know how to generate coding

ok now my website url is http://www.diyklick.com/

so check it and tell me how its going

about page speed and some modification

and also about canonical tags
« Last Edit: February 21, 2019, 03:10:55 pm by vivekshan »

patrickFromCroatia

  • Hero Member
  • *****
  • Posts: 763
Re: Redirect to category when item is deleted (solved)
« Reply #14 on: January 22, 2019, 12:48:47 am »
Glad it worked!
Some stuff can be solved with copying code, some needs a little work and some needs a whole plugin or more...

The site looks great. Only suggestion is to put some larger CSS and JS in their separate files if possible and then just put '<link rel="stylesheet" type="text/css" href="<?php echo osc_current_admin_theme_styles_url('new_css.css'); ?>" />' or '<script src="<?php echo osc_current_admin_theme_js_url('new_js.js'); ?>"></script>'.

If you will need some plugins or design, contact me. :)

Regards.