htaccess 获取额外的 php 个参数

htaccess getting additional php arguments

我的 .htaccess 代码

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

我的index.php代码如下

<?php

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

何时使用“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

如何获取参数 "a"($_GET['a'] )?

看看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=" [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

所以将您的 .htacess 规则更改为:

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