*

Ajit Sahane

  • ****
  • 204 posts
  • https://bestclassifiedsusa.com
city region old url redirect to new automatcily
« on: May 29, 2025, 11:48:33 AM »
When website osclass reinstall, then old city & regions url changed

Old city url --> abcd.com/xyz-c111111   &

Old region url --> abcd.com/pqr-r111111


here we can add 301 manual in htaccess, but when hole website redevelop then its not possible to add all thousand urls

So, any dynamic solution for this.

like
all old city & region urls match their name-ids & fine new relevant matching and auto 301 redirected

like this

Old city url --> abcd.com/xyz-c111111  ---- 301 ---> new city url  abcd.com/xyz-c121212

Old region url --> abcd.com/pqr-r111111 ------ 301 ----> new region url --> abcd.com/pqr-r121212


I am try this code -

Code: [Select]
<?php
// City & Region old URL to new 301 SEO redirect
$request = trim($_SERVER['REQUEST_URI'], '/');

if (
preg_match('/^([a-z0-9-]+)-(c|r)(\d+)$/', $request, $matches)) {
    
$slug = $matches[1];
    
$type = $matches[2];
    
$old_id = (int) $matches[3];

    if (
$type === 'c') {
        
// --- Handle City ---
        
$cityList = City::newInstance()->listAll();
        if (!empty(
$cityList)) {
            foreach (
$cityList as $city) {
                
$city_slug = strtolower(str_replace(' ', '-', $city['s_name']));
                if (
$city_slug === $slug && $city['pk_i_id'] != $old_id) {
                    
$new_url = osc_base_url() . "{$city_slug}-c{$city['pk_i_id']}";
                    
header("HTTP/1.1 301 Moved Permanently");
                    
header("Location: $new_url");
                    exit();
                }
            }
        }

    } elseif (
$type === 'r') {
        
// --- Handle Region ---
        
$regionList = Region::newInstance()->listAll();
        if (!empty(
$regionList)) {
            foreach (
$regionList as $region) {
                
$region_slug = strtolower(str_replace(' ', '-', $region['s_name']));
                if (
$region_slug === $slug && $region['pk_i_id'] != $old_id) {
                    
$new_url = osc_base_url() . "{$region_slug}-r{$region['pk_i_id']}";
                    
header("HTTP/1.1 301 Moved Permanently");
                    
header("Location: $new_url");
                    exit();
                }
            }
        }
    }
}
?>

seems to working but want to confirm check by MB team.
Pls look into this. Either simple plugin or function code want to do this dynamically. Website migration - updation - revamp this issue want to fix. So old ranking URL's help to keep SEO value and Users also find easily new page instead of dead 404 pages.
« Last Edit: May 29, 2025, 12:28:17 PM by Ajit Sahane »

*

MB Themes

Re: city region old url redirect to new automatcily
« Reply #1 on: May 29, 2025, 11:23:10 PM »
I woukd just copy location tables from old installation to new one.
  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots

*

Ajit Sahane

  • ****
  • 204 posts
  • https://bestclassifiedsusa.com
Re: city region old url redirect to new automatcily
« Reply #2 on: May 30, 2025, 06:37:11 AM »
actually old location table city list far old - since 2014 and fresh osclass install then import latest, so city & regions are updated & want updated data. also this location table copy must done very early step without new users or ads, but once new users & ad data fulfill then already new location city urls indexed on google.

after some time this solution not possible, so 301 only possible with correct way. once 2-4 month all old url crawled to new then either we can keep active this 301 or disable.

So, this solution want auto & ON / OFF mode type via plugin or osclass core function setting option.

I think lot of users expected this feature. It's help to redesign & upgrade from scratch old to new osclass setup without losing SEO benefit.

Hope this feature will update in upcoming version.

Thanks.

*

MB Themes

Re: city region old url redirect to new automatcily
« Reply #3 on: May 30, 2025, 05:10:45 PM »
No such feature is on plan.
  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots