Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have urls structured like this
I want to be able to match the first letter of the query "in this case perks" and make a url like this
I would also like it so that if the query starts with a number EX query "1 perk" that it will make a url like this.
Does anyone know how to achieve the matching of the first character of the search to make urls structured like this? Thanks

share|improve this question
up vote 1 down vote accepted

As the mod_rewrite documentation explains,

If you wish to match against the hostname, port, or query string, use a RewriteCond with the %{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables respectively.

Therefore, the solution is:

RewriteCond %{QUERY_STRING} (^|&)query=(([^&])[^&]*)
RewriteRule ^/search.php /%3/%2?

By the way, the + decodes to a space character only if it is in a query string. A space in the path portion of a URL should be encoded as %20; a + in the path portion of the URL should generally be interpreted literally.

share|improve this answer
Thanks a lot, much appreciated. – chris Apr 25 '12 at 23:52

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.