Access GET Data (Query Strings) in htaccess mod_rewrite

Posted in Web Development on 9 July 2011 1 comment

If you need to know how to do basic and common URL rewriting, please take a look at our SEO Friendly URL Rewriting post.

Use RewriteCond

We were used to use RewriteRule to match a URL, and then rewrite it. However, RewriteRule will always ignore your query string (GET data) so we need to use RewriteCond to capture the values in the query string.

This is the syntax:

RewriteCond %{QUERY_STRING} ^(.*)$

  • %{QUERY_STRING} captures the matched query string into % variable.
  • %1 will carry the matched content of the first group of brackets.
  • The second part ^(.*)$ is the pattern you want to match.
  • ^ means “start with”
  • $ means “end”
  • Like regular expressions, the dot in (.*) matches any characters.
  • The asterisk * inside the brackets is a quantifier that match 0 occurence to infinite occurences.
  • Therefore, combining them, (.*) matches any characters
  • So, clearly, in this example, it matches the whole query string part.

Some common ways of capturing query strings

Case 1

This case is for you if you’ve rewrote your url this way, for example:
FROM site.com/show.php?id=seo TO site.com/show/seo/

And you WANT to pass or append some GET data using the rewritten URL, like:
site.com/show/seo/?lang=en&source=firefox you want it to append like site.com/show.php?id=seo&lang=en&source=firefox

This is the htaccess rules:

?View Code HTACCESS
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^show/(.*)/$ show.php?id=$1&%1
  • This RewriteCond captures whole query string (first bracket) into %1
  • In this case, the captured string %1 is “lang=en&source=firefox”
  • We then do our normal URL rewriting and append the %1 to the URL after a ampersand (&)
Case 2

This case is for you if you know specifically or want to control what query string you are going to use.

For example, you only want to match lang=xxx and NOTHING ELSE, for example:
site.com/show/seo/?lang=en&more=1 you want it to append like site.com/show.php?id=seo&lang=en, ignoring other unknown query string.

?View Code HTACCESS
RewriteEngine On
RewriteCond %{QUERY_STRING} ^lang=(\w+)$
RewriteRule ^show/(.*)/$ show.php?id=$1&lang=%1
  • This RewriteCond captures lang‘s GET data (first bracket) into %1
  • \w matches word characters
  • The plus + means one occurrence or more
  • In this case, the captured string %1 is “en”
Case 3

Similar as Case 2, but tis case we have more query to match.

For example, you only want to match lang=xxx and time=000, for example:
site.com/show/seo/?lang=en&time=100 you want it to append like site.com/show.php?id=seo&lang=en&time=100

?View Code HTACCESS
RewriteEngine On
RewriteCond %{QUERY_STRING} ^lang=(\w+)&time=(\d+)$
RewriteRule ^show/(.*)/$ show.php?id=$1&lang=%1&time=%2
  • This RewriteCond captures lang‘s GET data (first bracket) into %1
  • This RewriteCond also captures time‘s GET data (second bracket) into %2
  • \w matches word characters
  • \d matches decimals
  • The plus + means one occurrence or more
  • In this case, the captured string %1 is “en” , %2 is “100″

Useful Link

Mod Rewrite : Rewritecond Documentation

 

Posted by Zen on 9 July 2011 • 23,170 visits 1 comment
Tags : ,


or Subscribe to specific category only :




  - 1 Comments


birkof says:

You need to pass [QSA] flag to your rewrite rule to send combined query strings.

Leave a Reply

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Previous Post
«
Next Post
»