我如何从以下网址获取 a 和 b 值:
http://127.0.0.1:8080/a:4522,b:846
我将此代码添加到.htaccess,以便将所有链接重定向到index.php,但在使用:
和,
字符时无法正常工作
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]
答案 0 :(得分:1)
对于http://127.0.0.1:8080/a:4522,b:846
,请求URI等于/a:4522,b:846
。并且.htaccess
指令将请求URI放入path
GET参数。因此,您只需在PHP中解析$_GET['path']
:
<?php
$vars = [];
if ($path = isset($_GET['path']) ? $_GET['path'] : null) {
foreach (explode(',', $path) as $part) {
$tmp = explode(':', $part);
if (count($tmp) == 2) {
$vars[$tmp[0]] = $tmp[1];
}
}
}
var_dump($vars);
输出
array(2) {
["a"]=>
string(4) "4522"
["b"]=>
string(3) "846"
}
<强> /etc/apache2/vhosts.d/01_test_vhost.conf 强>
<VirtualHost 127.0.0.12:80>
ServerName apache-test.local
ServerAdmin root@localhost
DocumentRoot "/var/www/apache-test.local/public"
ErrorLog "/var/www/apache-test.local/logs/error.log"
CustomLog "/var/www/apache-test.local/logs/access.log" common
<Directory "/var/www/apache-test.local/public">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
SetHandler application/x-httpd-php
</Directory>
</VirtualHost>
<强> /var/www/apache-test.local/public/.htaccess 强>
就像你的问题一样。
<强> /var/www/apache-test.local/public/index.php 强>
上面的PHP代码。