使用 .htaccess 重写路径变量


Rewrite Path Variable with .htaccess

我正在创建需要与某些PC软件通信的内容。当PC软件调用脚本int需要包括PHP需要读取的服务器上文件的用户名和相对路径时。

这是 PHP 在 get.php:

$Username = $_GET['Username'];
$RelativePath = $_GET['Path'];

未重写的 URL 如下所示:

http://localhost/get.php?Username=C8WgtdmAytNhGcq&Path=folder/Test.txt

现在我想把它重写为:

http://localhost/get/C8WgtdmAytNhGcq/folder/Test.txt

但我有问题,因为路径包含斜杠。

如何在路径中使用多个文件夹重写它?

您可以在站点根目录 .htaccess 中使用此规则:

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^get/([^/]+)/(.+)$ get.php?Username=$1&Path=$2 [L,QSA,NC]

.+将匹配任何字符的 1 个或多个,包括/