Keeping $_GET Variables With .htaccess Rewrite

July 4, 2014 .htaccess, Development

Keeping $_GET Variables With .htaccess Rewrite

I recently added a new feature to Internapse that allows visitors to switch between the default feed view and the optional headlines view on the results page.

To do this, I wanted to add a ?headlines variable to the URL so visitors could potentially bookmark the optional view.

When I tried to add the $_GET[‘headlines’] variable to the URL – http://www.internapse.com/movies/?headlines – it was not accessible in the code. The variable wasn’t even coming through when I printed out the $_GET array in PHP using print_r($_GET). Turns out the problem was actually in the .htaccess file and the URL rewrite.

The solution was only four characters long and one of those is a comma separator. The other three, added to the bracket portion of the rewrite rule, are QSA which stand for QueryString Append. It doesn’t take long to realize this acronym is the right tool for the job.

RewriteRule ^(.*)/$ /results.php?s=$1 [QSA,L]

Thanks to Kevin Stricker on Stack Overflow for the quick explanation.

Let's Talk About Your New Website

View Portfolio Contact Today