Removing GET parameters in a redirect
A trick to remove all GET parameters from a redirect very easily.
Today we are going to talk about a simple trick to remove GET parameters from a redirect using the redirect rules in the .htaccess file.
Often, to maintain our SEO ranking, we want to set up permanent 301 redirects from a company’s old website — one that was built with manual coding or an obsolete content manager — to the company’s new site, which we have migrated to WordPress, Joomla, Drupal or similar to make it easier for the marketing department to maintain all the content, and our old site has URLs like this:
http://www.midominio.com/noticias/ejemplo.html?id=2&cat=5
In the new version of the site, where we have created a news and current-affairs section, we want to redirect all these old addresses to the main blog URL, for example:
http://www.midominio.com/actualidad/
The problem is that if we go to the .htaccess and do a normal redirect:
RewriteRule ^noticias.html$ /actualidad/ [L,R=301]
If we type the old address in the browser, the result will be the following:
http://www.midominio.com/actualidad/?id=2&cat=5
which can cause errors and, very likely, generate 404 errors on our page, which we want to avoid at all costs.
How to remove GET parameters via .htaccess?
The trick to solve this problem is very simple, and consists of adding a ? at the end of the redirect, which will cause all the GET parameters in the original address to be ignored. So, if in the .htaccess we add the following line:
RewriteRule ^noticias.html$ /actualidad/? [L,R=301]
The result for all the pages that were in the news section will be the following:
http://www.midominio.com/actualidad/
and we will have correctly redirected all the old news items to our new blog, keeping all the links the old site had in the new content management tool and thus not losing points in the site’s SEO ranking.
We audit and fix WordPress and Laravel installations every week.