Chris Tonkinson Chris Tonkinson - 5 months ago 25
PHP Question

mod_rewrite with question marks and ampersands (with PHP)

I have a PHP-based web app that I'm trying to apply Apache's mod_rewrite to.

Original URLs are of the form:

And I'd like to transform these into:

Note that while rewriting the page name, I'm also effectively "moving" the question mark. When I try to do this, Apache happily performs this translation:

RewriteRule ^/([a-z]+)\?(.+)$ /index.php?page=$1&$2 [NC,L]

But it messes up the
variables in PHP. For example, a call to
yields only one
variable (
page => home
). Where did
x => 88
go? However, when I change my rule to use an ampersand rather than a question mark:

RewriteRule ^/([a-z]+)&(.+)$ /index.php?page=$1&$2 [NC,L]

a call like
will work just as I'd expect it to (i.e. both the
$_GET variables are set appropriately).

The difference is minimal I know, but I'd like my URL variables to "start" with a question mark, if it's possible. I'm sure this reflects my own misunderstanding of how mod_rewrite redirects interact with PHP, but it seems like I should be able to do this (one way or another).

Thanks in advance!




Try this:.

RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^/([a-z]+)(?:$|\?(?:.+)) /index.php?page=$1 [NC,L,B,QSA,NE]

B escapes the backreference (shouldn't be necessary since it is matching [a-z]+, but in case you want to extend it later, it might be useful).

EDIT: added RewriteCond. EDIT 2: QSA takes care of adding the ampersand.