---
title: "Learn about .htaccess 301 Redirect for URLs with a query string"
description: "Occasionally, you may need to redirect a URL that includes a query string. This will require that you use the RewriteEngine in order to have the redirect rule properly bring your viewers to the..."
url: https://www.inmotionhosting.com/support/website/301-redirect-query-string/
date: 2017-02-13
modified: 2025-06-12
author: "InMotion Hosting Contributor"
categories: ["Website"]
type: post
lang: en
---

# Learn about .htaccess 301 Redirect for URLs with a query string

Occasionally, you may need to **redirect** a URL that includes a **query string**. This will require that you use the *RewriteEngine* in order to have the redirect rule properly bring your viewers to the correct page. Follow the directions in the tutorial below to create a redirect rewrite rule for a specific page. The redirect will work with a query string.

## How to Identify a URL using a query string

You will need to first identify those instances where you need to use this type of rewrite rule. It's very easy to identify URLs with a query string. You will see a question mark after the page reference and then a value will be expressed. For example:

`https://sample-site.com/querypage.php?id=5`

The **query string** occurs after the question mark. These values are the result of the query that may generate a particular page. In this example, you will need to rewrite the URL so that the new domain is used while keeping the query values. The code presented below is the 301 re-write for needed redirect using the query string. In this we're simply rewriting the domain to "new_domain.com" while adding the value of the query string to the end of the URL.

RewriteEngine On
RewriteCond %{REQUEST_URI} ^\/querypage\.php$
RewriteCond %{QUERY_STRING} ^id=([0-9]*)
RewriteRule ^(.*)$ https://new_domain.com/querypage/%1

**What is this code?** The "RewriteCond % at the end of the statement.

*RewriteCond %{QUERY_STRING}* identifies the query string within the URL. In this case, it is "id=5". The *regular expression* is used to identify the value starts the statement with a carat symbol (^), then ends it with a "$".

The final part of the redirect for your URL with a query string is the actual rewrite rule. This is the section that changes the URL as long as the rewrite conditions are met.

`RewriteRule ^(.*)$ https://new_domain.com/querypage/%1 `

The beginning of the rewrite rule - *^(.*)$* - identifies what you're changing, which is the total URL. The second part - *https://new_domain.com/querypage%1* - shows what is being rewritten. Note that the "%1" portion identifies the value matched in the query string ({QUERY_STRING}) condition.

The final part of the rule - - indicates the type of redirect being used "R=301" and the flag "L" which basically means that this is the end of the rule. The rewrite will stop processing at this point.

Now that you understand the different parts of the actual rewrite code let's review the process of writing the Rewrite for a query string.

## How to write the 301 Redirect for a page with a query string URL

1. [![RewriteEngine-on](https://www.inmotionhosting.com/support/wp-content/uploads/2017/09/query-string-redirect_rewrite-query-string-1.jpg)](/support/wp-content/uploads/2017/09/query-string-redirect_rewrite-query-string-1.jpg) Your rewrite code should start with **RewriteEngine On**. This assumes that the mod_rewrite module for your Apache server is active. *NOTE: The **mod_rewrite module** is active on InMotion Hosting servers by default*.
2. [![request URI](https://www.inmotionhosting.com/support/wp-content/uploads/2017/09/query-string-redirect_rewrite-query-string-2.jpg)](/support/wp-content/uploads/2017/09/query-string-redirect_rewrite-query-string-2.jpg) Determine the URL being requested that triggers the rewrite. For example, this condition can be a specific and unique page within the URL that you wish to redirect. In the example used in this tutorial - *RewriteCond %{REQUEST_URI} ^/querypage\.php$* - the requested URI is specified to "querypage.php".
3. [![query string](https://www.inmotionhosting.com/support/wp-content/uploads/2017/09/query-string-redirect_rewrite-query-string-3.jpg)](/support/wp-content/uploads/2017/09/query-string-redirect_rewrite-query-string-3.jpg) The next line of the rewrite is the portion that identifies the value used in the query string - *RewriteCond %{QUERY_STRING} ^id=([0-9]*)*. This query matches any numeric value provided for the ID. It is later referenced in the rewrite rule with "%1".
4. [![](https://www.inmotionhosting.com/support/wp-content/uploads/2017/09/query-string-redirect_rewrite-query-string-4.jpg)](/support/wp-content/uploads/2017/09/query-string-redirect_rewrite-query-string-4.jpg)Finally, the fourth line in the code is the rewrite rule. The rewrite is not executed unless the previous listed conditions are met. In this case, here's the rewrite rule:`RewriteRule ^(.*)$ https://new_domain.com/querypage/%1 `The re-written URL will look like the following:`https://new_domain.com/querypage/5`Once the URL has been rewritten, it will be sent to the browser and displayed in place of the old URL and the user will be redirected to that new location. As per the last part of the rule, this is a 301 redirect and it is then flagged to terminate at that point.

This should provide you with enough information to create your own 301 redirect for pages using a query string. There are many ways to make redirects and also many ways to write the regular expression portion of the redirect. If you are not familiar with using regular expression, then our recommendation is to consult with an experienced developer. Make sure that you make a [backup of your site](/support/edu/cpanel/cpanel-backups/) before making any changes to your website files.
