1

I have made a seo friendly url works fine but i need to remove something from url like this example

example.com/download/bla-bla-bla/2"?id=1........"

i can get the link with this

$getremover = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

so i wanna delete the quote part include ?id with php i have to use get insted of post method because as i have heard google doesnt crawl post method greetings.

1 Answers1

0

If you want to do it "manually", you can get only the url part before the ? using explode it and retain only the first part:

echo $getremover = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]".explode('?', $_SERVER['REQUEST_URI'])[0];

This returns an array containing all part of the string divided by ?:

explode('?', $_SERVER['REQUEST_URI'])

With [0] you get only the first value of the array = the first part of the string before the ?:

explode('?', $_SERVER['REQUEST_URI'])[0]

Otherwise, there is a dedicated function to do that: parse_url.

parse_url — Parse a URL and return its components

$url = 'http://username:password@hostname:9090/path?arg=value#anchor';

var_dump(parse_url($url));
var_dump(parse_url($url, PHP_URL_SCHEME));
var_dump(parse_url($url, PHP_URL_USER));
var_dump(parse_url($url, PHP_URL_PASS));
var_dump(parse_url($url, PHP_URL_HOST));
var_dump(parse_url($url, PHP_URL_PORT));
var_dump(parse_url($url, PHP_URL_PATH));
var_dump(parse_url($url, PHP_URL_QUERY));
var_dump(parse_url($url, PHP_URL_FRAGMENT));

The above example will output:

array(8) {
  ["scheme"]=>
  string(4) "http"
  ["host"]=>
  string(8) "hostname"
  ["port"]=>
  int(9090)
  ["user"]=>
  string(8) "username"
  ["pass"]=>
  string(8) "password"
  ["path"]=>
  string(5) "/path"
  ["query"]=>
  string(9) "arg=value"
  ["fragment"]=>
  string(6) "anchor"
}
string(4) "http"
string(8) "username"
string(8) "password"
string(8) "hostname"
int(9090)
string(5) "/path"
string(9) "arg=value"
string(6) "anchor"
user2342558
  • 5,567
  • 5
  • 33
  • 54