0

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'] ) ?

Alok Patel
  • 7,842
  • 5
  • 31
  • 47
Manoj Kumar
  • 63
  • 1
  • 4

1 Answers1

1

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]
Community
  • 1
  • 1
Ben
  • 8,894
  • 7
  • 44
  • 80