By using this site you agree to the use of cookies.

Learn more

Share via:

Blocking Out Non-Existent URL Parameters

One of the best way to prevent duplicate content, is to simply throw an error when a non-existent GET parameter was requested.

309 views

Edited: 2018-01-24 08:26

404 on non-existent query strings.

URL Parameters is the part of the URL after the Question Mark (?), usually they are used by PHP to deliver content based on what is entered as the parameter value. Inside PHP scripts, they may be accessed through the $_GET super global. But what can we do, when we do not want certain parameters available?

Ideally, parameters not in use by your application should not be accessible, and throw an error. When it comes to URL parameters, I prefer to throw a 404 Not Found error for non-existent parameters. Doing this has the benefit of totally getting rid of the problem with duplicate content.

I have created a PHP script for this purpose, which you may want to include in your own CMS. The script is shown below:

$url = parse_url($_SERVER['REQUEST_URI']); // Parses the URL and places the result in an array.
$pieces = explode('&', $url['query']); // Split by parameter separator (&)

$defined_url_parameters = array();
$defined_url_parameters['some_page_id'] = '';
$defined_url_parameters['another_parameter'] = '';
$defined_url_parameters['third_url_parameter'] = '';

foreach ($pieces as &$value) {
    if (strpos($value, '=') !== false) {
        $url_parameter = strstr($value, '=', true);
    } else {
        $url_parameter = $value;
    }
    if(!isset($defined_url_parameters["{$url_parameter}"])) {
      header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
      echo 'My king. Nothing by that name is known in this world.';exit();
    }
}

Checking if a URL parameter exists

For the script to work, you will have to redefine the URL parameters that are being used by your application. I suggest you keep the $defined array in a settings file somewhere, so that you will not clutter your code with settings.

In the script above, we are are defining the URL parameters in the $defined_url_parameters array. To check if a requested parameter exists, we simply perform a if (!isset...){} check. The exclamation mark means "not", so if the array index is undefined, we will throw a 404 error.

if(!isset($defined_url_parameters["{$url_parameter}"])) {
  header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
  echo 'My king. Nothing by that name is known in this world.';exit();
}

The exit(); in this case is required, since we are working inside a loop. The loop itself will iterate over the different parameters in the query string.

Canonical URLs

Those of you who do not have access to the PHP code of your CMS can instead include a rel=canonical URL, which was made with this problem in mind. It will also be easier if you are not very good at altering PHP code yourself.

The rel=canonical URL should be placed in the <head> part of the HTML. The HTML code looks like this:

<link rel="canonical" href="http://example.com/this-is-my-article">

You should replace the example URL with the URL of your article.

Comments