If the URL is the following :
If: http://www.imvu-e.com/products/dnr/
Then: http://www.imvu-e.com/products/dnr/
If: http://www.imvu-e.com/products/dnr/?
Then: http://www.imvu-e.com/products/dnr/
If: http://www.imvu-e.com/products/dnr/index.php
Then: http://www.imvu-e.com/products/dnr/
If: http://www.imvu-e.com/products/dnr/page.php?var=2
Then: http://www.imvu-e.com/products/dnr/
If: http://www.imvu-e.com/products/dnr
Then: http://www.imvu-e.com/products/
How can I do this?
My attempt:
print "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['REQUEST_URI'])."/";
Have a look at parse_url() function.
It returns anything you need.
Simply print_r() the result from parse_url to see what you get back. You probably want something like:
The issue with your "attempt" is that $_SERVER['REQUEST_URI'] will contain everything the user passed, including index.php and question mark and possibly more. In order to get what you are after, you need to parse the
$_SERVER['REQUEST_URI']
:/
, leave it as it ithttp://
(orhttps://
with the domain name)Ended up going with this
Inspired in part by Erwin Moller's answer (Why I voted it) and snipplets across web.
You can strip everything from the last backslash till the end of the string. I am pretty sure that
dirname($_SERVER['REQUEST_URI'])
won't do the job. You can also try usingdirname($_SERVER['SCRIPT_FILENAME'])
. The last shoud work if you don't have some fancy .htaccess rewrite rules.