PHP query string without question mark

If you want to pass variables in the URL without having to use ?var=value format, then you will need .htaccess file and a URL RewriteRule.

For example, let’s say we want to pass a variable via the query string http://www.phpdebutant.com/var1=value1, then we would use a .htaccess file and place it in the public_html folder.

The .htaccess file would like this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.+)=(.+)$ /index.php?$1=$2

Notice the use of regular expression:

^ denotes the beginning of the line
$ denotes the end of the line

We use ( ) to enclose a regular expression match, so the value of the first match would be in $1, and second would be in $2.

The RewriteRule says that whatever query matches that regular expression, we would redirect the request to /index.php?$1=$2 where $1 and $2 are the values of the match.

One Response to “PHP query string without question mark”

  1. sorrybut says:

    Actually, you’re wrong.

    $_SERVER['QUERY_STRING'];

    You can form query strings however you want. If you stray from the default, though, it’s your responsibility to parse and interpret them…PHP won’t do it for you. That’s all.

    So http://example.com/foo=bar|bar=foo

    Could easily be translated in PHP to $foo = ‘bar’ and $bar = ‘foo’.