Clean your WordPress Permalinks for Better SEO
WordPress is pretty smart at resolving URLs even if you add any number of extra parameters to the permalinks.
To give you an example, if a WordPress site is located at example.com, all the following URLs will work fine as the various parameters found in the Query String (like the utm_source parameter added by Google Analytics) will simply get ignored by WordPress.
1\. http://example.com/?partner=nyt
2. http://example.com/?src=dlvr.it
3. http://example.com/?utm_source=feedburner&utm_medium=feed
These parameters are often added to WordPress URLs by external services that aren’t in your control – for instance, URL shorteners or services that feed your blog to social sites.
Why Clean your WordPress URLs?
Why should you even consider cleaning up your WordPress URLs when these parameter do no harm? Well, here are a a few reasons:
#1. Technically, the URL example.com/?src=blog is not the same as example.com/?src=feed which is not the same as example.com – the real canonical version. Thus if someone were to bookmark or share these URLs on social sites, it would be really hard for you to consolidate the social media metrics.
#2. The second reason is Google. If you open the HTML Suggestions section of your Google Webmaster Tools dashboard, you may come across pages that are found to duplicate title tags (and thus duplicate content). The underlying URLs are same but since they have different parameters, Google may sometimes confuse them as separate pages. You definitely want to avoid this situation.
How to Remove Extra Parameters from WordPress URLs
You cannot prevent other services from adding new parameters to your URLs but you can always redirect these URLs to the ‘clean’ versions so that there exists only one version of your URLs on the Internet.
There are two ways do that:
1. The JavaScript version: Open the header.php file of your WordPress template and copy-paste the following code somewhere inside the <head>
tag.
<?php if(!is_search()) { ?>
<script type="text/javascript">
var url = window.location.href;
if ( url.split('?').length >= 2 ) {
window.location = url.split("?")[0];
}
</script>
<?php } ?>
The code checks the incoming URL request for any parameters and if found, it will simply strip all the parameters from the URL and reloads the WordPress page with the clean URL.
The above approach uses JavaScript and will therefore be mostly invisible to Google. It also won’t solve any of the previously mentioned problems so let’s try something else.
2. The .htaccess version: Open the .htaccess file of your domain and copy-paste the following code above the mod_rewrite rules that were added by WordPress. You don’t have to change any code in your PHP templates.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteCond %{QUERY_STRING} !^(s|p)=.*
RewriteCond %{REQUEST_URI} !.\*wp-admin.*
RewriteRule ^(.*)$ /$1? [R=301,L]
</IfModule>
If you have never used mod_rewrite before, the above might look a bit confusing but it’s actually quite simple.
It first ignores URLs that have ‘s’ or ‘p’ parameters since we don’t want to remove any of the Post IDs, like labnol.org/?p=19028
, or the search parameters like labnol.org/?s=pdf
from the WordPress URLs. It also ignores request coming from the WordPress admin (wp-admin)dashboard. Finally, we do a 301 redirect thus also passing all the Google Juice to the clean and canonical URL.
There’s one important thing to note here. Any functionality that depends on URL parameters will obviously not work if you remove those parameters. That should not be a problem for most WordPress sites, but do keep that in mind before making the change.
Amit Agarwal
Google Developer Expert, Google Cloud Champion
Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.
Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory