happyhardik
happyhardik

Reputation: 25567

url of the current page without page name and query string

As mentioned in the question, I want to get the url of the page without the page name and query string.

For example my url is:
http://sub.domain.com/app/page.php?var=abc

what I want from this is:
http://sub.domain.com/app/
without the query string and page name.

I found this tutorials:
http://www.phpf1.com/tutorial/get-current-page-url.html
it was helpful but not exactly what I want.

Thanks for your answers.

Upvotes: 2

Views: 6014

Answers (5)

Muhammad Tahir
Muhammad Tahir

Reputation: 2485

Exactly answer that you are looking for is

$protocol = 'http'.(!empty($_SERVER['HTTPS']) ? 's' : '');
$currURL = $protocol.'://'.$_SERVER['SERVER_NAME'].substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/'));

=======================================
Explanation

For Example your current url is

http://sub.domain.com/app/page.php?var=abc

it will return

http://sub.domain.com/app

Upvotes: 0

andy
andy

Reputation: 51

The dirname(__FILE__) is not the same as happyhardik's goal.

substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/'))

returns: /wwwroot/activetest2

dirname(__FILE__) 

returns: /home/content/94/3671394/html/wwwroot/activetest2

To get the full URL:

$_SERVER['SERVER_NAME'].substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/'))

returns: www.domainname.com/wwwroot/activetest2

Upvotes: 5

jwueller
jwueller

Reputation: 30991

I suggest the following trick:

$location = dirname(__FILE__);

If you need an absolute URL, add the following in front of it:

$protocol = 'http'.(!empty($_SERVER['HTTPS']) ? 's' : '');
$root = $protocol.'://'.$_SERVER['SERVER_NAME'];

Upvotes: 2

Tom Vervoort
Tom Vervoort

Reputation: 534

The server vars don't hold only the path without the page name.

So you need to strip the page name from PHP_SELF (everything after the last / should do the trick)

Upvotes: 1

Chandresh M
Chandresh M

Reputation: 3838

you can use HTACCESS for this problem. so by using htaccess your URL can be changed.

Thanks.

Upvotes: 0

Related Questions