gadss
gadss

Reputation: 22509

PHP how to get clean url from $_SERVER['REQUEST_URI'] with no parameters

is it possible that I can only get the clean url with no parameters.

this is an example.

$dis_url = $_SERVER['REQUEST_URI'];
echo $dis_url;

returns into:

'/mysite.com/subdir?message=value&message2=value2'

is it posible that it will return into:

'/mysite.com/subdir'

the parameter will not be included?

does anyone have an idea about my case? thanks in advance...

Upvotes: 1

Views: 8302

Answers (6)

Kenny Ming
Kenny Ming

Reputation: 56

To add to previous replies.

You can directly get the path from parse_url() by directly passing the constant PHP_URL_PATH as second parameter.

$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

The variable $uri will contain the path only '/subdir'.

From the php manual: PHP: parse_url() - Manual

Upvotes: 0

str_replace( $_SERVER['REQUEST_QUERY'], '', $_SERVER['REQUEST_URI'] );

Upvotes: 0

Swaraj Giri
Swaraj Giri

Reputation: 4037

Try parse_url()

    $parts = parse_url($_SERVER['REQUEST_URI']);
    $required = $parts['path'];

Upvotes: 1

Amit Verma
Amit Verma

Reputation: 41229

Use parse_url() function

<?php
  $dis_url ="http://example.com/mobile?u=me";
  $url=parse_url($dis_url);
  $scheme=$url["scheme"];
  $host=$url["host"];
  $path=$url["path"];
  echo $scheme."://".$host.$path;
?>

Output: http://example.com/mobile

Source : http://php.net/manual/en/function.parse-url.php

Upvotes: 1

Michael Doye
Michael Doye

Reputation: 8171

Use strtok and trim

$dis_url = $_SERVER['REQUEST_URI'];
$uri = trim(strtok($dis_url, '?'));

Upvotes: 4

AkiEru
AkiEru

Reputation: 788

$dis_url = $_SERVER["SCRIPT_NAME"];
echo $dis_url;

It will only shows the PHP File name itself.

Upvotes: 0

Related Questions