简体   繁体   中英

PHP - Get URL and remove the trailing slash

So I've created this function that get's the URL of the Website, but I also want it to remove the trailing / as well.

So far I have this:

function base_url() {
    $base_url = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';

    return $base_url.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}

But I don't know how to remove the trailing / from the URL.

I've also tried to add the rtrim but still no luck:

function base_url() {
    $base_url = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
    $base_url = rtrim($base_url, '/');

    return $base_url.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}

Image of the URL -

在此处输入图片说明

Try it like this:

function base_url() {
    $base_url = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
    $base_url.= '://'.$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

    return rtrim( $base_url, '/' );
}

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