Manoj Kumar Manoj Kumar - 1 year ago 63
PHP Question

htaccess getting additional php arguments

My .htaccess code

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?key=$1

My index.php code given below


$key=isset($_GET['key']) ? $_GET['key'] : 'home';
if( in_array( $key, array('home','about','terms') ) ){

When is using "http://localhost/project_dir/home" is working correctly(home is assigned to argument '?key'). but i want to pass extra arguments like "http://localhost/project_dir/home?a=abc123"

How can i get argument "a"(
) ?

Ben Ben
Answer Source

Have a look at flags - in particular, QSA:


When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.

Consider the following rule:

RewriteRule "/pages/(.+)" "/page.php?page=$1" [QSA]

With the [QSA] flag, a request for /pages/123?one=two will be mapped to /page.php?page=123&one=two. Without the [QSA] flag, that same request will be mapped to /page.php?page=123 - that is, the existing query string will be discarded.

Apache manual, © 2016 The Apache Software Foundation, Apache License 2.0

So change your .htacess rule to:

RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?key=$1 [QSA]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download