Umesh Awasthi
Umesh Awasthi

Reputation: 23587

Get Request URI PHP

I am struck in getting the URI in my wordpress application and lack of PHP knowledge is making my progress slow. I have this URL

http://abc.com/my-blog/abc/cde 

i need to create a URL something like

http://abc.com/my-blog/added-value/abc/cde

where http://abc.com/my-blog is the URL of my wordpress blog which i can easily get using following method

home_url()

i can use PHP $_SERVER["REQUEST_URI"] to get request URI which will come up as

/my-blog/abc/cde

and than i have no direct way to add value as per my requirement

is there any way to achieve this easily in PHP or Wordpress where i can get following information

  1. Home URL
  2. Rest part of the URL

so that in end i can do following

Home-URL+ custom-value+Rest part of the URL

My point of Confusion

  1. On my local set up $_SERVER["REQUEST_URI"] is giving me /my-blog/abc/cde, where /my-blog is installation directory of wordpress and i can easily skip first level.
  2. On production server its not same as /my-blog will not be part of the URL.

Upvotes: 2

Views: 14559

Answers (5)

smarteist
smarteist

Reputation: 1421

This is a reliable way to get current url in PHP .

    public static function getCurrentUrl($withQuery = true)
    {
        $protocol = stripos($_SERVER['SERVER_PROTOCOL'], 'https') === false ? 'http' : 'https';
        $uri = $protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

        return $withQuery ? $uri : str_replace('?' . $_SERVER['QUERY_STRING'], '', $uri);
    }

Upvotes: 0

Carl Owens
Carl Owens

Reputation: 1292

You might want to use explode or some other sting function. Some examples below:

  $urlBits = explode($_SERVER["REQUEST_URI"]);

  //blog address
  $blogAddress = $urlBits[0];

  //abc
  $secondPartOfUri = $urlBits[1];

  //cde
  $thirdPartOfUri = $urlBits[2];

  //all of uri except your blog address
  $uri = str_replace("/my-blog/", "", $_SERVER["REQUEST_URI"]);

Upvotes: 0

Madara's Ghost
Madara's Ghost

Reputation: 174947

Very briefly:

<?php

$url = "http://abc.com/my-blog/abc/cde";

$parts = parse_url($url);

$path = explode("/", $parts["path"]);
array_splice($path, 2, 0, array("added-part")); //This line does the magic!

echo $parts["scheme"] . "://" . $parts["host"] . implode("/",$path);

Upvotes: 2

George
George

Reputation: 36786

OK, so if $addition is the bit you want in the middle and $uri is what you obtain from $_SERVER["REQUEST_URI"] then this..

$addition = "MIDDLEBIT/";
$uri = "/my-blog/abc/cde";

$parts = explode("/",$uri);
$homeurl = $parts[1]."/";
for($i=2;$i<count($parts);$i++){
    $resturl .= $parts[$i]."/";
}
echo $homeurl . $addition . $resturl;

Should print:

my-blog/MIDDLEBIT/abc/cde/

Upvotes: 0

Samuel Cook
Samuel Cook

Reputation: 16828

You can store the home url in a variable, using wordpress, using get_home_url()

$home_url = get_home_url();

$custom_value = '/SOME_VALUE';

$uri = $_SERVER['REQUEST_URI'];

$new_url = $home_url . $custom_value . $uri;

Upvotes: -1

Related Questions