title: Change ugly query string to a prettier /path/to/page/ pattern
body: <p>I have a site which uses a custom query string to deliver pages to the visitor. A sample query string looks like <code>/?sec=news&amp;pg=current&amp;bg=203</code>. I'd like to use <code>.htaccess</code> to rewrite the strings into a pattern like <code>/news/current/203</code>. </p>
  
  <p>I know how to take the <code>/news/current/203</code> URL and make it into a query string that I can parse with PHP. I typically use this bit of htaccess code: </p>
  
  <pre><code>RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} -f [OR]
  RewriteCond %{REQUEST_FILENAME} -d
  RewriteRule ^(.+) - [PT,L]
  RewriteRule ^(.*) /index.php?id=$1 [L]```
  </code></pre>
  
  <p>And my PHP then explodes <code>id</code> on the <code>/</code> and off we are running. </p>
  
  <p>My question is this... How can I add a few more lines to this htaccess code that will allow the old URL query pattern to continue to work for people that have bookmarks or links that use the <code>/?sec=news...</code> pattern? Basically, it seems like I need to take this old query string and combine the values into one string that I can pass to index.php in the <code>id</code> parameter. I don't want to lose the ability to honor the old pattern, but also need to promote the new cleaner path-based string. </p>
  
  <p>I know there is some regex that can help here, but I am terrible at understanding regular expressions. Any help would be appreciated, and let me know if I am not making any sense. </p>
  
  <hr>
  
  <p>Update: The answer provided makes my final htaccess file look like this...</p>
  
  <pre><code>RewriteEngine on
  
  # Prevent directory listings
  Options All -Indexes
  
  # Rewrite to www
  Options +FollowSymLinks
  RewriteCond %{HTTP_HOST} ^sample.com[nc]
  RewriteRule ^(.*)$ http://www.sample.com/$1 [r=301,nc]
  
  # WHEN DONE TESTING&lt; CHANGE ALL 302 to 301 !!!
  # Honor the old format /index.php?sec=XXX&amp;pg=XXX&amp;bg=XXX and turn it into the new format
  RewriteCond %{QUERY_STRING} sec=(\w+)&amp;pg=(\w+)&amp;bg=(\d+)
  RewriteRule ^(.*)$ /%1/%2/%3? [R=302,L]
  
  # Turn /path/to/page into index.php?id=path/to/page
  RewriteCond %{REQUEST_FILENAME} -f [OR]
  RewriteCond %{REQUEST_FILENAME} -d
  RewriteRule ^(.+) - [PT,L]
  RewriteRule ^(.*) /index.php?id=$1 [L]
  </code></pre>