apache redirect with query string

How to Redirect with Query String in Apache

Apache allows you to redirect URLs in numerous ways. Sometimes you may need to redirect with query strings in Apache. In this article, we will look at how to redirect URLs alongwith query string using .htaccess file. We will look at different use cases that involves redirecting URLs with query string.


How to Redirect with Query String in Apache

Here are the steps to redirect with query string in Apache. Before you proceed, please ensure that you have enabled .htaccess file in Apache. Here are the steps to enable .htaccess in Apache.


1. Open .htaccess file

Open .htaccess file in a text editor

$ sudo vi /var/www/html/.htaccess


2. Redirect with Query String

There are multiple ways to do this. We will look at each of them one by one. Basically, we will use Apache’s Redirect/RedirectMatch statements to redirect URLs, depending on the situation.

If you want to redirect only 1 URL with query string /product.php to /new_product/php, add the following statement.

Redirect /product.php http://example.com/new_product.php

If you want to redirect a group or folder of URLs with query strings, you need to RedirectMatch. Here is an example where we redirect /folder to /new_folder along with query strings in each URL.

RedirectMatch ^/folder/(.*)$ http://example.com/new_folder/$1

In the above case, any URL in /folder such as /folder/new-project will be redirected to corresponding URL in /new_folder such as /new_folder/new-project.

However, if you want to redirect based a query string, then you cannot use the above methods. Let us say you want to redirect all URLs with query string id=<number> then you need to add something like the following to .htaccess file. For example, in the following case we convert a query string value into a URL string, that is, we direct /page.php?id=1 to /product/1 and /page.php?id=10 to /product/10

RewriteEngine On
RewriteCond %{REQUEST_URI}  ^/page\.php$
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^(.*)$ http://example.com/product/%1.pdf [R=302,L]

Let us look at the above lines in detail. The first line enabled mod_rewrite. The second line uses RewriteCond to check if the requested URL is /page.php. You can change it as per your requirement. This is to ensure that other URLs with same query string are not redirected.

Next, we use RewriteCond to check if the requested URL contains id query string with a numeric parameter value. The last line uses RewriteRule to redirect matching URLs to /product/ where the URL string after /product is the parameter value in requested URL.


3. Restart Apache Server

Restart Apache server to apply changes.

$ sudo service apache2 restart

In this article, we have learnt how to direct with query string in Apache. You can modify the above configuration as per your requirements.

Also read :

How to Check if Cookie is Set in Apache Server
How to Redirect Apache Based on Cookie
How to Redirect in Apache Based on Hostname
How to Block URL Pattern in Apache
How to Redirect Port 80 to 8080 in Apache

Leave a Reply

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