It's not as easy as it might sound to find out if an URI is identical or not, especially as you take the query parameter into account here.
One common way to do this is to have a function that normalizes the URL and then compare the normalized URIs:
$url1 = 'http://example.com/page.php?tab=items&msg=3&sort=title'; $url2 = 'http://example.com/page.php?tab=items&sort=title&msg=3'; var_dump(url_nornalize($url1) == url_nornalize($url2)); # bool(true)
Into such a normalization function you put in your requirements. First of all the URL should be normalized according to the specs:
function url_nornalize($url, $separator = '&') { // normalize according RFC 3986 $url = new Net_URL2($url); $url->normalize();
And then you can take care of additional normalization steps, for example, sorting the sub-parts of the query:
// normalize query if applicable $query = $url->getQuery(); if (false !== $query) { $params = explode($separator, $query); sort($params); $query = implode($separator, $params); $url->setQuery($query); }
Additional steps can be though of, like removing default parameters or not allowed ones, or duplicate ones and what not.
Finally the string of normalized URL is returned
return (string) $url; }
Using an array/hash-map for the parameters isn't bad as well, I just wanted to show an alternative approach. Full example:
<?php /** * http://stackoverflow.com/questions/27667182/are-two-urls-identical-ignore-the-param-order */ require_once 'Net/URL2.php'; function url_nornalize($url, $separator = '&') { // normalize according RFC 3986 $url = new Net_URL2($url); $url->normalize(); // normalize query if applicable $query = $url->getQuery(); if (false !== $query) { $params = explode($separator, $query); // remove empty parameters $params = array_filter($params, 'strlen'); // sort parameters sort($params); $query = implode($separator, $params); $url->setQuery($query); } return (string)$url; } $url1 = 'http://EXAMPLE.com/p%61ge.php?tab=items&&&msg=3&sort=title'; $url2 = 'http://example.com:80/page.php?tab=items&sort=title&msg=3'; var_dump(url_nornalize($url1) == url_nornalize($url2)); # bool(true)
parse_urlandparse_str. Either comparing the param arrays, or sort'em and reassemble urls.parse_str, sort them withksort, then put them back. Then you can compare them.