0

I have url like this http://example.com/dir1/dir2/dir3/dir4/Page.php?charts.htm and I need to remove the page/extension before the querystring so it outputs http://example.com/dir1/dir2/dir3/dir4/?charts.htm

$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$temp = explode( '?'  , $url );
$temp[0] = ""; //remove part before the ?

Not even sure if I'm on the right track here

3
  • you're kind of on the right track if you want the page to redirect to the url without Page.php in the name. if you want this done dynamically on each request without a redirect on page load, then you need to use .htaccess Commented Nov 18, 2015 at 17:04
  • This will be output on a page, not a redirect. Just need to show the new url with the Page.php removed Commented Nov 18, 2015 at 17:05
  • 2
    See parse_url() Commented Nov 18, 2015 at 17:06

2 Answers 2

1

try this :

$url = str_replace("Page.php", "", $url);
Sign up to request clarification or add additional context in comments.

Comments

1

You can use a combination of parse_url() and dirname()

$url = "http://example.com/dir1/dir2/dir3/dir4/Page.php?charts.htm";
$urlArray = parse_url($url); // Array ( [scheme] => http [host] => example.com [path] => /dir1/dir2/dir3/dir4/Page.php [query] => charts.htm )

$newURL  = $urlArray["scheme"]."://".$urlArray["host"]."/";
$newURL .= dirname($urlArray["path"])."/?".$urlArray["query"];
// gives http://example.com/dir1/dir2/dir3/dir4/?charts.htm

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.