removing query strings from canonical URL - php

I use PHP to echo the current URL in <link rel="canonical" href="URL" /> on my static website, but unfortunately it also echoes query strings along with it.
http://www.example.com?querystring gets echoed in canonical, and I'm trying to find a away to echo URLs ignoring query strings and whatever follows it.
I am currently using .
Is there a way I can declare all the necessary code for this once in a global template, and simply echo $url wherever I require the URL without query strings?

Let's say you ahve your URL in variable. Then we will explode the url with delimiter "?" and then echo only the first index of returned array:
$url = http://www.example.com?querystring;
$array = explode("?", $url);
$yourUrl = $array[0];
There you go

for example you can use parse_url and re-build your query
$url = "http://www.example.com?querystring";
$newUrl = parse_url($url);
echo "http://".$newUrl['host'].$newUrl['path'];
etc

use request.forwardURI to set canonical uri, it removes the query string paramaters

Related

How to delete tracking code from links in PHP

Hi I have a form in WordPress where users can submit a link to a product, but very often the links come with unnecessary baggage, like tracking codes. I would like to create a filter in WordPress and clean the links so they consist of just a working link. I would like to if possible confirm that the link still works or a method that will guarantee that the link will still work.
The main things I want to get rid of in links are utm_source and it's contents, utm_medium and it's contents, etc. Everything but the clean working link.
So for example, a link like this:
https://www.serenaandlily.com/variationproduct?dwvar_m10055_size=Twin&dwvar_m10055_color=Chambray&pid=m10055&pdp=true&source=detail&utm_source=affiliate&utm_medium=affiliate&utm_campaign=pjdatafeed&publisherId=20648&clickId=2669312134#fo_c=745&fo_k=c0ebaf8359ca7853df8343e535533280&fo_s=pepperjam
Will end up like this:
https://www.serenaandlily.com/variationproduct?dwvar_m10055_size=Twin&dwvar_m10055_color=Chambray&pid=m10055
I'd really appreciate if someone can lead me in the right direction.
Thanks!
You can do what you want with explode, parse_str and http_build_query. This code uses an array of unwanted parameters to decide what to delete from the query string:
$unwanted_params = array('utm_source', 'utm_medium', 'utm_campaign', 'clickId', 'publisherId', 'source', 'pdp', 'details', 'fo_k', 'fo_s');
$url = 'https://www.serenaandlily.com/variationproduct?dwvar_m10055_size=Twin&dwvar_m10055_color=Chambray&pid=m10055&pdp=true&source=detail&utm_source=affiliate&utm_medium=affiliate&utm_campaign=pjdatafeed&publisherId=20648&clickId=2669312134#fo_c=745&fo_k=c0ebaf8359ca7853df8343e535533280&fo_s=pepperjam';
list($path, $query_string) = explode('?', $url, 2);
// parse the query string
parse_str($query_string, $params);
// delete unwanted parameters
foreach ($unwanted_params as $p) unset($params[$p]);
// rebuild the query
$query_string = http_build_query($params);
// reassemble the URL
$url = $path . '?' . $query_string;
echo $url;
Output:
https://www.serenaandlily.com/variationproduct?dwvar_m10055_size=Twin&dwvar_m10055_color=Chambray&pid=m10055
Demo on 3v4l.org
You can do this in the PHP itself. There is a function called parse_url() (https://secure.php.net/manual/en/function.parse-url.php) which can give you all the URI params as array. After parsing, you can filter the parameters, remove the unwanted. Finally, use http_build_query() (https://secure.php.net/manual/en/function.http-build-query.php) to build a string URI to return :)

$_GET, Ampersand is interrupting

Trying to use a $_GET['url'] variable to grab data from a URL:
http://mysite.com/?url=http://this.is/?q=an&?example=url
What I want above is bolded, but sadly the $_GET['url'] will only get "http:// this.is/?q=an" because the & makes it interpret it as the beginning of a new variable within the URL.
Is there a way to ignore the ampersands so my script can get the entire URL I need it to? The URL that is appended to ?url= is not within my limits to control so most work but some do contain the dreaded &. After reading questions on Stack Overflow I'm not holding out much hope :(
If you have absolutely no control over the arguments placed on the query string (for whatever reason), you can also do this by manually parsing the $_SERVER['QUERY_STRING'] varible, e.g.
$page = str_replace("url=", "", $_SERVER['QUERY_STRING']);
Of course, if possible, you should encode it using the answers posted by everyone else.
Use urlencode()
$get_url = urlencode('http://this.is/?q=an&?example=url');
$url = 'http://mysite.com/?url=' . $get_url;
If you can't control the query string, you could use
$query = $_SERVER['QUERY_STRING'];
$pos = strpos($query, "url=");
if ($pos !== false) {
$url = substr($query, $pos + 4);
}
This code returns everthing after url=
If you have the possibility,you should encode the url with urlencode to create a clean url.
If you don't do this, you get eventually an server error, with strange urls and you can't pass more than one url(because everything after the url= is interpreted as url)
Here is the tested code:
if (!empty($_GET['url'])) {
echo $_GET['url'].'<br />';
}
$url = urlencode('http://this.is/?q=an&?example=url');
echo 'LINK';
Hope it will help you.

Is it possible to get information from this type of url: website.com/hello/richard

For example, if there is a url like www.website.com/hello/richard, would it be possible to echo hello and 100 separately onto my page.
eg:
hello how are you today richard
You can get the data from $_SERVER['REQUEST_URI'] and then do whatever you like with it.
Yes it would be. Try this:
$myURL = $_SERVER['REQUEST_URI'];
$myTokens = explode('/', $myURL);
echo $myTokens[1] . "blah" . $myTokens[2];
This code gets the current URL into the myURL variable, then it calls a function called explode which turns it into an array based on the position of the '\' character. Then it echos out certain elements of that array. If you play around with output using echo you will soon see for yourself what is going on.
Sure that's possible. You can get URL as a string using $_SERVER['request_uri]. Then you might want to use explode function to firm array of strings where delimiter is /. Then you may parse it. Or you can do this via .htaccess using rewrite rule

Encoding $_GET variable URL ignores ampersand "&" character

So im trying to get an url from the address bar that looks like this:
http://mysite.com/url.php?name=http://test.com/format.jsp?id=738ths3&secure=false
I'm using the $_GET variable to read it right off the URL my code is as follows
$arc = rawurlencode($_GET['name']);
echo "URL: $arc";
This only returns
URL: http://imgur.com/format.jsp?id=738ths3
It 's missing the &secure=false
What i want it to look:
URL: http://test.com/format.jsp?id=738ths3&secure=false
I have tried urlencode, rawurlencode with no avail, i have looked in google a number of forums and stackoverflow none of the answer help, any ideas? Thanks!
urlencode shows this:
URL: http%3A%2F%2Ftest.com
so i cant have that either!
You'll need to urlencode() before constructing the URL, ie:
$url = "http://mysite.com/url.php?name=".urlencode('http://test.com/format.jsp?id=738ths3&secure=false');
This way, you will be able obtain the full URL as a name GET parameter from $_GET['name'].
Explanation:
Without urlencode() it when constructing the URL, PHP would treat is as 2 separate parameters, separated by &:
$_GET['name']
which is http://imgur.com/format.jsp?id=738ths3 for your case
$_GET['secure']
which is false for your case
Alternatively:
From your comment, it seems that you do not have control for the URL construction. You can get the full $_GET in a single string using http_build_query:
$name = http_build_query($_GET);
You would then obtain:
echo $name; // name=http://test.com/format.jsp?id=738ths3&secure=false
// which you would then may want to strip away the first 'name='
$name = substr($name, strlen('name='));
echo $name; // to obtain http://test.com/format.jsp?id=738ths3&secure=false
The original URL, http://mysite.com/url.php?name=http://test.com/format.jsp?id=738ths3&secure=false, contains two query-string parameters: name and secure. The & in the query-string belongs to the full URL, not the URL in the name parameter.
If you have control over this value, when declaring the link/URL, use PHP's urlencode() to encode the full name value, such as:
$url = "http://mysite.com/url.php?name=" . urlencode("=http://test.com/format.jsp?id=738ths3&secure=false");
This will properly encode the name parameter and your $arc = $_GET['name']; will work as desired.
If you do not have control over setting the value and are simply parsing something you're receiving, you can split the given string on the name= parameter and assume everything else after it is part of name:
$splitQuery = split('name=', $_SERVER['REQUEST_URI']);
$arc = $splitQuery[1];
To decode the encoded URL, after you've accessed it, use PHP's urldecode():
$arc = urldecode($_GET['name']); // assuming you're properly encoding the `name` parameter
If you cannot encode the URL, you can get the current URI with this code:
$url = $_SERVER["REQUEST_URI"];
that in your case, the $url is :
/url.php?name=http://test.com/format.jsp?id=738ths3&secure=false
Then you can split it with explode and validate it and take the GET params from it.

PHP URL in query string using GET

I have a URL like:
http://www.google.com/test.html?d=1232&u=32
and I want to add it as a part of a GET query string like:
http://www.mysite.com/index.html?a=123&d=http://www.google.com/test.html?d=1232&u=32
Note the double "d" used. I want the URL sent to be just a url and not be read for it's query string...
What is the best way to do this to avoid problems?
You can use the urlencode() function.
Example:
$url = 'http://www.mysite.com/index.html?a=123&d='
. urlencode('http://www.google.com/test.html?d=1232&u=32');
You can use urlencode() to put that in the URL without having it interfere with anything else you have in there.
URL-encode the second url:
http://mysite.com/index.html?a=123&d=<?php echo urlencode('http://google.com/etc..'); ?>
You can assign a url to a variable and have it be query-string safe by using urlencode() (http://us3.php.net/urlencode). So you could do:
$url = 'http://www.mysite.com/index.html?a=123&d=' . urlencode('http://www.google.com/test.html?d=1232&u=32');
In this example the query-string var 'd' now houses all the contents of the second url. You will have to urldecode() it on the receiving end in order to extrapolate the data.

Categories