Book Now
Advanced Technical SEO

Redirecting Parameters in Apache

Learn how to perform all types of parameter redirections in Apache to optimise your website for search engines.

Redirecting Parameters in Apache
Author:
Carlos Sánchez
Topics:
servers
Publication Date:
2024-08-28

Last Review:
2024-09-18

Although I have developed some excellent tools for server-side redirections, they still need improvement and the addition of new features. This is currently in progress.

One of the current shortcomings of this tool is when it comes to redirecting with parameters, as neither mod_alias nor RedirectMatch recognise what would be a Query String (commonly known as a parameter). Fortunately, it is possible to carry out a wide range of parameter redirections using the marvellous mod_rewrite. I will provide some examples which, once understood and adapted to specific needs, can be used for bulk parameter redirection in a quick and convenient manner.

What is a parameter?

To understand how to redirect parameters, it’s necessary to have a basic knowledge of what they are and how they function. However, to keep this post simple, I will explain the concept briefly.

https://carlos.sanchezdonate.com/?everything-after-the-question-mark

Any time there is a URL with a question mark, it is a parametrised URL, and everything that follows the question mark can be considered the parameter.

By default, a parameter in a URL does not alter the content of the page, meaning it will display the same content despite being a different URL. This is why it’s important to configure a Self-Canonical.

Carlos Sánchez.

What are parameters used for?

Parameters serve countless functions. They can be used to: sort content; filter content; restrict content; segment by region; track information; paginate content; segment by language; perform A/B testing; optimise cache content, and even force certain crawling tools, among other applications.

They are a powerful ally for both a web developer and a Technical SEO specialist.

How do parameters affect SEO?

In SEO, parameters are far more significant than the use of the G Search Console’s parameter tool. We must pay close attention to them, especially for analytics, which is why even Google developed a parameter generator (to make tracking with Analytics easier).

We must bear in mind that, although Google is increasingly better at understanding them, they can still deliver entirely different content or even translations. Therefore, it’s crucial to have a comprehensive understanding of meta tags and to have a self-canonical in place, in case unexpected parameters are added to our website, so they are not treated as a different content URL.

Theory of Parameter Redirections in htaccess

All the codes I present here should go in the .htaccess file in the ROOT of the project, assuming you are using Apache as your server. It is always advisable to make all changes in a test environment beforehand to ensure that no contradictory directives are given to the server itself.

All these redirections will have a code similar to this one. I present this as a template example to explain how it works, and then we will move on to specific examples.

<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} ^/contacto/
RewriteCond %{QUERY_STRING} ([a-zA-Z0-9])
RewriteRule (.*) $1otraurl-%1/ [R=301,L,QSD]
</IfModule>

All these redirections will go inside an <ifmodule mod_rewrite.c>, assuming the RewriteEngine On code has been placed beforehand.

It is essential to consider the order of the codes in the .htaccess file to avoid conflicts.

The main commands we will use are:

%{REQUEST_URI}: Used to indicate which URL we are referring to, i.e. the URL to be redirected.

%{QUERY_STRING}: Used to indicate what should be in the parameter to be redirected.

RewriteRule: Used to indicate the destination of the redirection.

With this and a little bit of Regex, real magic can be done.

%1: Returns the value of the matches captured in the RewriteCond. The 1 indicates the Regex Match group number.

$1: Returns the value of the affected URI. The 1 indicates the Regex Match group number.

([a-zA-Z0-9]): All alphanumeric characters.

(.*): All characters.

In any case, I will show a few examples to demonstrate the potential these redirections have, which can serve multiple purposes, especially in migrations.

I advise testing regular expressions with Regex101. And bear in mind whether there are any special characters or spaces in the URL you want to redirect.

Redirecting parameters to other parameters

Parameter A to Parameter B

<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} parametroA
RewriteRule (.*) $1?ParametroB [R=301,L,QSD]
</IfModule>

This will redirect any URL containing the parameter: parametroA to parametroB

Changing Parameter A to Parameter B with a specific URL

<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} ^/contacto/
RewriteCond %{QUERY_STRING} parametroA
RewriteRule (.*) /contacto/?ParametroB [R=301,L,QSD]
</IfModule>

When you only want to change one word in the parameter

Imagine the Campaign URL Builder for ads was configured incorrectly, and instead of google, gogle was used, which is disrupting all analytics (I mention this case among many others that could happen).

A solution would be as follows:

<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} utm_source=gogle(.*)
RewriteRule (.*) $1?utm_source=google%1 [R=301,L,QSD]
</IfModule>

This way, across the entire website, all URLs using that version of the parameter will be redirected to the correct one, changing from:

http://sanchezdonate.test/contacto/?utm_source=gogle&utm_medium=cpc&utm_campaign=jobs&utm_id=pruebaa

to:

http://sanchezdonate.test/contacto/?utm_source=google&utm_medium=cpc&utm_campaign=jobs&utm_id=pruebaa. This solution allows you to correct typos in certain filters en masse.

Now, it may be that we are looking to replace absolutely every parameter containing the word gogle with google, replacing just that word (without having to copy everything from the start to that word for replacement).

This would be the most optimal solution:

<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} (.*)(gogle)(.*)
RewriteRule (.*) $1?%1google%3 [R=301,L,QSD]
</IfModule>

Remember, you can play with all these values along with everything mentioned in the post. Therefore, you can create infinite types of combinations. You can designate this to occur only on a specific URL, for example.

Changing parameters with value to another

This is useful for fixing errors in the Campaign URL Builder or when there is a change in how filters or previous development work.

<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} ^/brooks/glycerin-20/
RewriteCond %{QUERY_STRING} key=value-([0-9]+)
RewriteRule (.*) /brooks/glycerin-20/?talla-%1 [R=301,L,QSD]
</IfModule>

In this way, we will change from https://example.com/brooks/glycerin-20/?key=value-44 to https://example.com/brooks/glycerin-20/?talla-44.

Redirecting Parameters to Different URLs

Moving parameters to another URL and avoiding a loop

As always, with Request_URI, we can specify that it is for a specific page, or if we do not specify, it will work for any page.

Let’s take an example. We want all URLs coming from Facebook, which has a ?fbclid= parameter, to be redirected to our Linktree for some reason, but we want to keep the parameter for tracking purposes.

We can make it so that "anything containing fbclid" is moved to linktree, keeping the parameter. However, if the parameter is kept, it will redirect to our linktree indefinitely, causing a "too many redirects" error.

To avoid a loop, we could use a "does not contain" in the Request URI, so that "anything not linktree with the fbclid parameter will be moved to linktree, bringing the parameter along."

<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(linktree)/ [NC]
RewriteCond %{QUERY_STRING} (fbclid)(.*)
RewriteRule (.*) /linktree/?fbclid%2 [R=301,L,QSD]
</IfModule>

This would move everyone with the fbclid parameter except the linktree directory; to include it, remove the !.

Every URL containing a specific word in its parameter is moved to this page

It may happen that we want all URLs with a specific parameter to go to a particular URL, either because they come from an advertisement we have no control over or due to a web configuration issue.

<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} anuncios [NC]
RewriteRule (.*) /landingpage/ [R=301,L,QSD]
</IfModule>

In this case, any page with the parameter ?anuncios or containing the word anuncios would go directly to https://example.com/landingpage/.

Converting parameters to subdirectories

It might happen that we find a website where the entire architecture was made with parameters, such as a real estate website, and we have managed to change it to use folders instead.

How would we make this change in the most correct way?

In this scenario, we can imagine an online real estate website where, for example, rental properties in Cartagena would have a URL like this:

https://example.com/?alquiler-viviendas-murcia-cartagena and we want it to redirect, but also work with all provinces and cities without having to list them all manually, so that with a new architecture it would look like this: https://example.com/alquiler/viviendas/{province}/{city}.

This would be the way:

<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} alquiler-viviendas-(.*)-(.*)
RewriteRule (.*) /alquiler/viviendas/%1/%2? [R=301,L]
</IfModule>

In this case, I included the ? in the RewriteRule to avoid retaining parameters. In this way, we can obtain some pretty functional redirections.

For Apache 2.4 or higher versions, it would be more appropriate to remove the question mark and add the QSD flag as I did in all the examples.

<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} alquiler-viviendas-(.*)-(.*)
RewriteRule (.*) /alquiler/viviendas/%1/%2 [R=301,L,QSD]
</IfModule>

Redirecting to the Parent URL

By this, I mean redirecting parameters to the URL itself without the parameter.

Redirecting all parameters to the parent URL on a specific URL

<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} ^/contacto/
RewriteCond %{QUERY_STRING} ([a-zA-Z0-9])
RewriteRule (.*) /contacto/ [R=301,L,QSD]
</IfModule>

With this example, we would redirect all parameters from https://example.com/contacto/?loquesea to https://example.com/contacto/, meaning without the parameter.

Redirecting a specific parameter on a specific URL to the parent URL

Let’s imagine we only want to redirect parameters that come with a specific nomenclature, for example, ?key=value from the contact page. This would be the way:

<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} ^/contacto/
RewriteCond %{QUERY_STRING} key=value
RewriteRule (.*) /contacto/ [R=301,L,QSD]
</IfModule>

With this example, we would redirect all parameters from https://example.com/contacto/?key=value to https://example.com/contacto/, meaning without the parameter, but https://example.com/contacto/?loquesea would remain the same.

Removing any parameter or a specific parameter on any URL

If the goal is to force a redirection of any parameter containing the word spam (for example) on any URL, it would be done like this:

<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} spam
RewriteRule (.*) $1 [R=301,L,QSD]
</IfModule>

Whereas, if the goal is to eliminate parameters from the entire website (not particularly recommended, but it may happen for some reason), it would be done like this:

<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} [a-zA-Z0-9]
RewriteRule (.*) $1 [R=301,L,QSD]
</IfModule>

Forcing 404 with parameters

There isn’t a strong SEO reason for performing this action, but if at some point it’s necessary to ensure no parameters exist on a specific URL, here’s how:

404 on any parameter of a specific URL

With this code, we would make all parameters on the contact page disappear, forcing a 404.

I’ve used [a-zA-Z0-9] in the Query String instead of (.*) because otherwise, the contact page itself would also return a 404 response code.

<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} ^/contacto/
RewriteCond %{QUERY_STRING} [a-zA-Z0-9]
RewriteRule ^ - [R=404]
</IfModule>

404 to parameters that match something specific

In this way, we could, for example, force a 404 on all parameters containing the word "facebook":

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} facebook
RewriteRule ^ - [R=404]
</IfModule>

Adding parameters to a URL

This practice can be very useful in cases where you want to parameterise an image for reasons like user cache, and the website’s code doesn’t yet allow it easily.

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/img/imagen\.jpg$
RewriteCond %{QUERY_STRING} !^parametro$
RewriteRule ^img/imagen\.jpg$ /img/imagen.jpg?parametro [R=302,L,QSD]

Note the importance of the query_string in this case, as by using the exclamation mark "!" we are adding an exception, so that the rewriterule does not work when the parameter is literally "?parametro". In this way, as we are ultimately redirecting all possible paths of that image to its version with the "parametro" parameter, we avoid a chain of redirections.

Important Note: WordPress has a very specific configuration within the wp-content folder; this type of redirection will not work within that folder unless specific hooks are added due to the core of the CMS itself and how it is designed.

Configurations

With all the examples presented here, plus the explanations, innumerable combinations can be made for any specific case.

ALL CODES PRESENTED HERE HAVE BEEN TESTED AND VERIFIED BEFORE PUBLICATION TO AVOID ERRORS. However, I am not responsible for any poor implementations made due to lack of knowledge.

For all these examples, Apache version 2.4.47 has been used, and all the codes work correctly.

Bibliography

Query String Reds by Jeff Starr

If you like this article, you would help me a lot by sharing my content:
Interested in Advanced SEO Training?

I currently offer advanced SEO training in Spanish. Would you like me to create an English version? Let me know!

Tell me you're interested
You might be interested in other articles:
SEO Articles
Usamos cookies para asegurar que te damos la mejor experiencia en nuestra web. Aquí tienes nuestra política de privacidad.