简体   繁体   中英

Apache rewrite rule url parameters

I am trying to capture all the parameters sent in the url and redirect pass it to a php file. Here is my rewrite rule

RewriteCond %{REQUEST_METHOD} ^GET
RewriteRule ^/locations/(\w+\d*)+ /php/listone.php?name=$1 [L]

While this works correctly in the case when there are no white spaces in the url:

GET /locations/example HTTP/1.0

The $_GET['name'] in listone.php will contain example, but when I send

GET /locations/ex1 ex2
GET /locations/ex%20ex2
GET /locations/ex1+ex2 

In listone.php I gave the following:

 $name = $_GET['name'];
 echo $name;
 $name = str_replace("+", " ", $name);

I will get just ex1. How can I capture all of them?

Change your RewriteRule to this:

 RewriteRule ^/locations/(.+) /php/listone.php?name=$1 [L]

You aren't matching any character like white-space, % and + .

You might try a different apprach, and catch requests from the $_SERVER['REQUEST_URI'] , and conditionally catch $_GET parameters, if needed.

Use this mod_rewrite snippet to achive this:

Options +FollowSymLinks
IndexIgnore */*

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d

RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /mydirectory/index.php [NC,L]

Just send a simple request, like:

http://hostname.com/mydirectory/test/123

And echo the $_SERVER['REQUEST_URI'] variable:

die($_SERVER['REQUEST_URI']); // outputs: "/mydirectory/test/123"

In this way you can create a simple PHP router for your requestss

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM