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 -
<?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.