Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you make .htaccess pass all GET parameters without knowing what they are named?

Tags:

php

.htaccess

I am working on a CMS that involves custom user pages. I have a hierarchy with three levels set up, and it works pretty well. It's got a .htaccess that makes the URLs friendlier, too:

RewriteRule ^([A-Za-z0-9]+).html /content.php?page=$1 [L]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+).html /content.php?page=$2 [L]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+).html /content.php?page=$3 [L]

That code works just fine.

Another feature that I have is adding custom php page, in case I need to add something more powerful (like make a contact form that gets email addresses from a database).

So my question is how could I modify (or add to) the .htaccess to make all of the GET parameters passed to the PHP page. For example, to use the contact page example, if the user goes to http://www.mysite.com/contact.html?name=someuser, I want the .htaccess to rewrite it as content.php?page=contact&name=someuser, but without specifically telling it to look for 'name' as the parameter name. I would also like it to work with as many parameters as possible.

I know this is a big (and hopefully not confusing) request, but is it possible? Or at least parts of it? Thanks!

like image 777
Zak Avatar asked Feb 21 '11 22:02

Zak


2 Answers

add QSA to your parameters. so for your first rule it would become

RewriteRule ^([A-Za-z0-9]+).html /content.php?page=$1 [L, QSA]
like image 188
ehudokai Avatar answered Oct 18 '22 22:10

ehudokai


You'll need to use the QSA (query string append) flag. The mod_rewrite documentation has more detail.

like image 31
Harper Shelby Avatar answered Oct 18 '22 22:10

Harper Shelby