简体   繁体   中英

How to check if URL contain range of numbers and redirect the page with PHP

Google indexed some pages that i need to redirect to another page, There is 2 kind of redirects that need to be done:

  1. redirect url with specific parameters - this have been done.

  2. redirect url that contain number from 861-1015 as part of the URL for example http://www.siteurl/category/1004-page - this not done yet and i need help .

What I need is if user search in google and he will get as a result one of the 2 condition the page will redirect to http://www.siteurl/new-page .

this is the code i have now:

// CODE FOR REDIRECT PSALAMS
if ($_SERVER['HTTP_REFERER']=="https://www.google.co.il/" && 
($_GET['view']=="psalms" || $_GET['view']=="psalm" || 
$_GET['view']=="blessings" || $_GET['option']=="com_psalm") ){
header("HTTP/1.1 301 Moved Permanently"); 
header("Location: https://www.rabenu.co.il/he/תהילים");
die();
}

Thanks

function redirectGoogleIndexPage() 
{
    $path = $_SERVER['REQUEST_URI'];
    $redirectUrl = "http://www.siteurl/new-page";

    if (!$path)
        return;

    $parse_path = explode('/', $parse_url['path']);
    $last_path = end($parse_path);

    // here you can do any condition
    // use like functions preg_match or strpos or others for get right path

     /**
     * for example check last path "1004-page"
     * preg_match("/^[0-9]{4}-[a-zA-Z]*$/", $last_path)
     */
    $number_of_path =  preg_replace('/[^\d]+/', '', $last_path);

    if ($number_of_path >= 861 && $number_of_path <= 1015)
        header("Location: {$redirectUrl}");
}

redirectGoogleIndexPage();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM