Manoj Kumar Manoj Kumar - 4 months ago 6
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

<?php

$key=isset($_GET['key']) ? $_GET['key'] : 'home';
if( in_array( $key, array('home','about','terms') ) ){
include("$key.php");
}else{
include("profile.php");
}
?>


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"(
$_GET['a']
) ?

Ben Ben
Answer

Have a look at flags - in particular, QSA:

QSA|qsappend

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]