在带有.htaccess的PHP中使用斜杠作为GET变量

问题描述:

我目前正在尝试创建一个 .htaccess 文件,该文件实际上将其转换为:

I'm currently trying to create a .htaccess file that essentially converts this:

[发件人] http://www.example.com/pagename.php?1=name&2=电子邮件& 3 =哈希
[收件人] http://www.example.com/pagename/name/email/hash

然后可以在PHP中将其读取为 $ name = $ _GET [1];$ email = $ _GET [2] ,依此类推...

Which can then be read in PHP as $name = $_GET[1]; $email = $_GET[2] and so on...

其中 pagename 等于不带文件扩展名的文件名,然后将其后的每个斜杠设置为新的GET变量,其增量为1(从理论上讲,您可以定义无限的斜杠并将继续将这些 $ _ GET 变量加1.

Where pagename is equal to the filename without the file extension and then every trailing slash after that is set as a new GET variable incrementing by one (in a way that you could theoretically define unlimited trailing slashes and it would continue to increment these $_GET variables by one.

有人知道吗?

如果希望有任意数量的变量,则需要打开 Multiviews ,然后将一些代码添加到php脚本中查看 $ _ SERVER ['PATH_INFO'] 变量.像这样:

If you want there to be an arbitrary number of variables, you need to turn on Multiviews then add some code to your php scripts to look at the $_SERVER['PATH_INFO'] variable. So something like this:

$data = explode("/",trim($_SERVER["PATH_INFO"],"/"));

$length = count($data);
for ($i = 1; $i <= $length; $i++) {
  $_GET[$i] = $data[$i-1];
}

使用所有路径元素填充 $ _ GET 变量.

to populate the $_GET variable with all the path elements.

然后在htaccess中,您需要类似以下内容来附加 php 扩展名:

Then in htaccess, you need something like this to append the php extension:

RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)(/.+)$ /$1.php$2 [L]