PHP code to detect if a visitor uses the default, query based, WordPress permalink structure

If you changed the permalink structure of your WordPress blog from the default one (?p=n) to the so called “pretty permalinks”, perhaps you’d like to remove from the database of search engines all those addresses that followed the default permalink structure, in order to avoid duplicate content ‘punishments’.

A way to achieve this would be to detect if a page was opened from an address that follows the old permalinks and then write the appropriate “noindex” Robots meta tag.

You can detect pages of the default permalink structure because they contain queries, using a question mark in their address.

The following code will detect if an address includes a query and then it will write the proper meta tags to the header.

<?php if($_SERVER['QUERY_STRING'] != '') {

If you have some other advice for a successful move to a new permalink structure, leave a note below.