InMotion Hosting Support Center

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:

http://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 ^(.*)$ http://new_domain.com/querypage/%1 [R=301,L]


What is this code? The "RewriteCond %[REQUEST_URI}", "RewriteCond %{QUERY_STRING}" and "RewriteRule" are the Apache server commands. These commands require the mod_rewrite to be active. After each command is a regular expression used to identify or match part of the URL. For more information see the offical Apache documentation for Apache Module mod_rewrite (version 2.4).

Breakdown of the rewrite code for understanding:

RewriteEngine On - This enables the mod_rewrite engine module.

RewriteCond %{REQUEST_URI} This identifies the URL request submitted in the browser. The regular expression identifies the unique portion of the URL request with the expression ^/querypage\.php$. The ^ (carat) indicates the start and the $ indicates the end of the portion of the URL being identified. The "\" is used in front of any periods within the URL. Note that this condition is case sensitive. To make it case insensitive you would add the flag [NC] 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 ^(.*)$ http://new_domain.com/querypage/%1 [R=301,L]

The beginning of the rewrite rule - ^(.*)$ - identifies what you're changing, which is the total URL. The second part - http://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 - [R=301,L] - 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 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 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 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. 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 ^(.*)$ http://new_domain.com/querypage/%1 [R=301,L]

    The re-written URL will look like the following:

    http://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 before making any changes to your website files.

Support Center Login

Our Login page has moved, Click the button below to be taken to the login page.

Social Media Login

   
Social Login Joomla

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Ask a Question

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

0 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?

Search

Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!