John R Perry
John R Perry

Reputation: 4192

How to return a URL without a subfolder/subdirectory (using PHP not htaccess)

The question basically covers it. I'm sure this question has been asked, but I can't seem to find it.

Just to be clear, I have this url

https://www.example.com/subfolder/page.php

and I'd like to return with this url

https://www.example.com/page.php

I think I'm supposed to use http_build_url() but alas, I am here.

I know that this can be done with .htaccess, but for my specifically weird situation, I need it to be done in php.

Upvotes: 0

Views: 190

Answers (1)

Adviov
Adviov

Reputation: 480

Most simply, you could just redirect with:

header('location: example.com/page.php');

With http_build_url() as you wanted, you could do:

echo http_build_url("http://example.com/subfolder/page.php",
array(
    "scheme" => "http",
    "host" => "example.com",
    "path" => "/page.php"
),
HTTP_URL_STRIP_AUTH | HTTP_URL_JOIN_PATH | HTTP_URL_JOIN_QUERY | HTTP_URL_STRIP_FRAGMENT
);

Upvotes: 1

Related Questions