For example, if I have a URL http://example.com/?src=example&test_28934
How do I remove everything after ?, so the user always lands on http://example.com?
I have tried this and the URL stays the same.
$current_url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$current_url = reset((explode('?', $current_url)));
You should check this https://www.php.net/manual/en/function.substr.php
and this Remove portion of a string after a certain character
For your example it will be
$current_url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$current_url = substr($current_url, 0, strpos($current_url, "?"));
header("Location: " . "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . "?");
Related
I am using PHP to set the current URL as a variable using
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
This would echo the string:
http://www.example.com/landing-page-demo/
I would like to replace the 'landing-page' part of the string with 'confirmation-page' and then save this updated URL as another variable.
I was thinking of using str replace, is this the most ideal method of doing this? Not sure how to approach the problem
Indeed, short of knowing regular expressions, str_replace will do the trick.
Perform str_replace on $_SERVER['REQUEST_URI']
Example:
$url = 'http://' . $_SERVER['SERVER_NAME'] . str_replace("landing", "confirmation", $_SERVER['REQUEST_URI']);
My current solution using Str Replace:
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; // Full URL var
$redirecturl = str_replace("landing","confirmation", $url);
Using str_replace is the easiest solution however you can do your logic with pathinfo($url)
$url = "http://www.example.com/landing-page-demo/";
$newURL = str_replace('landing', 'confirmation', $url);
echo $newURL;
i have a location menu that has to change location, the good thing is every url exist in every city,, and every city is a subdomain
city1.domain.com.uk/index.php?page=category/238/12
city2.domain.com.uk/index.php?page=category/238/12
Im trying this. Im trying to break the URL to remove subdomain , so i can replace it for each item in menu
I want to get index.php?page=category/238/12
<?PHP
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https')=== FALSE ? 'http' : 'https';
$host = $_SERVER['HTTP_HOST'];
$script = $_SERVER['SCRIPT_NAME'];
$params = $_SERVER['QUERY_STRING'];
$url = $protocol . '://' . $host . $script . '?' . $params;
// break it up using the "."
$urlb = explode('.',$url);
// get the domain
$dns = $urlb[count($urlb)-1];
// get the extension
$ext = $urlb[count($urlb)+0];
//put it back together
$fullDomain = $dns.'.'.$ext;
echo $fullDomain;
?>
But i Get this php?page=category/238/12
Also i havent think in a solution for an issue i will be facing with this..
If im looking at a product the url change to something like
city2.domain.com.uk/index.php?page=item/preview/25
But, the products dont exist in every city , so my user will get a 404.
=(
How can i make a conditional in the process so if page=item/preview/25 i do replace this for
page=index/index
You can split the domain as:
$url = "city1.domain.com.uk/index.php?page=category/238/12";
list($subDomain, $params) = explode('?', $url);
list($domain, $sub) = explode('/', $subDomain);
$newUrl = $sub . "?" . $params;
echo $newUrl;
Cheers!
How about this:
<?php
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https')=== FALSE ? 'http' : 'https';
$host = $_SERVER['HTTP_HOST'];
$script = $_SERVER['SCRIPT_NAME'];
$params = $_SERVER['QUERY_STRING'];
$url = $protocol . '://' . $host . $script . '?' . $params;
$url=(parse_url($url));
$dns = substr($url['host'],stripos($url['host'],'.')+1);
$fullDomain =$url['scheme']."://".$dns.$url['path']."?".$url['query'].$url['fragment'];
if (substr($url['query'],stripos($url['query'],'=')+1,stripos($url['query'],'/')-stripos($url['query'],'=')-1)=='item') {
echo "redirect";
} else {
echo "don't redirect";
}
echo "<br>".$fullDomain;
?>
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
Home URL
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
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.
On production server its not same as /my-blog will not be part of the URL.
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);
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/
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"]);
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);
}
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;
How can I get my full current url in php but minus all querystrings?
Example
echo 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
Would echo something like assuming query strings were in place...
http://www.example.com/example?tab=foo&dslip=yes
How can I get the same as above but cut off all the query strings?
How is this done in php.
Thanks.
PHP_SELF is what you need I believe.
echo('http' . ((empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === 'off') ? '' : 's') . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF']);
Or the __FILE__ constant, depending on your exact configuration and situation.
Try this
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$part = explode('?',$url);
echo $part[0];
I think the solution is this:
echo 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'];
the $_SERVER['SCRIPT_NAME'] variable returns path of your script without the query string like you want.
if you like to see all variables available in php just try this
phpinfo();
Best regards
How can I get the full URL, like http://www.domain.com/page.php?id=someid&page=1, not just http://www.domain.com/page.php?
$_SERVER['QUERY_STRING'] has the query string portion of the URL.
A quick answer:
$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING'];