Menu Close

Two ways to manipulate URL query strings using RewriteRule in htaccess

0
(0)

Last Updated on June 18, 2021 by Amit

Query strings are part of URL that usually appear at the end or before fregment.
A typical query string looks something like the following :

?q=foo&bar=123

Query string in URL starts with a question mark and it contains query key and value parameters.
In the Query String above , q is a key and the foo is its value. Multiple Query key values are separated by an & character.
In this article I am going to show you some useful ways to manipulate query strings using RewriteRule directive. If you want to learn how to match against URL query strings using RewriteRule then you can read further because this post is written especially for URL rewriting beginners. I will show you two working mod_rewrite tricks to manage URL query strings.

Apache mod_rewrite has predefined methods to work with URL query strings

Mod_rewrite beginners often find it complicated to manage or Redirect URL query strings but in fact it’s not that hard. Mod_rewrite has predefined variables to work with it.
The following are the two variables we can use use to manipulate URL query strings :

  • %{QUERY_STRING}
  • %{THE_REQUEST}


%{QUERY_STRING}
is used to match against URL query string. You can use this variable to match against URL query strings.

The following is an an example of how we can test URL query string using RewriteCond directive :

RewriteCond %{QUERY_STRING} ^q=foo&bar=123$ [NC]

The condition above checks a static URL query string that looks something like the followin
?q=foo&bar=123 .
You can use Regular expressions to check query string values dynamically , the following is an example :

RewriteCond %{QUERY_STRING} ^q=.+&bar=.+$ [NC]

The example above matches any values in the query string.


%{THE_REQUEST}
represents the full URL requested by a web browser, you can also use this variable to check URL query string.
If we have the following URL :

example.com/file.php?q=foo
Then we can use the following condition to make sure the URL has the specified Query String

RewriteCond %{THE_REQUEST} \?q=foo\s [NC]

The “\s at the end of the condition pattern above matches a space char and it’s important to use when you are using this variable in RewriteCond as it works like a string boundary.

Redirect URL with Query String

Now you know that we can check query string in two ways on Apache mod_rewrite.
URL redirection with query string is easy .
Suppose we want to redirect our URL with a specific query string to another URL ,

We will learn to redirect :
example.com/file.php?q=foo&n=123
to a different URL , that looks something like :
example.com/demo.php
You can see the second URL is clean and it has no query string , we will use the following rule in our htaccess file for this redirection :

RewriteEngine on
RewriteCond %{QUERY_STRING} ^q=foo&n=123$ [NC]
RewriteRule ^file\.php$ https://example.com/demo.php? [R,L]

One important thing to take note of in the rule above is ? that is at the end of the target URL. This empty question mark is important otherwise the old query string will get appended to the target URL.

The following is the same redirection rule with %{THE_REQUEST}

RewriteEngine on
RewriteCond %{THE_REQUEST} \?q=foo&n=123\s [NC]
RewriteRule ^file\.php$ https://example.com/demo.php? [R,L]

Related articles

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

code