Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have urls structured like this http://domain.com/search.php?query=perks
I want to be able to match the first letter of the query "in this case perks" and make a url like this
http://domain.com/p/perks
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.
domain.com/1/1+perk
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

1 Answer 1

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

 
discard

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.