How to update URL and remove understore for the Cakephp

I need to update URL for the current my URL is users/member_login then i need to users/member-login

users/member_login => users/member-login

1 answer

  • answered 2018-10-09 17:48 Jimmix

    I don't know Cakephp but

    Here is a quick and dirty redirect if the url was ended with users/member_login it will redirect to url ended with users/member-login

    $url = $_SERVER['REQUEST_URI'];
    $subject = $url;
    
    $pattern = '#users/member_login$#';
    $match = preg_match($pattern, $subject);
    
    //redirect if match
    if ($match > 0) {
    
        $pattern = '#users/member_login#';
        $replacement = 'users/member-login';
        $newUrl = preg_replace($pattern, $replacement, $subject);
    
        header("HTTP/1.1 301 Moved Permanently");
        header("Location: $newUrl");
    
        exit;
    
    }