Php .htaccess将子域重写为请求参数

Php .htaccess将子域重写为请求参数,php,wildcard,subdomain,Php,Wildcard,Subdomain,我可以重写此请求类型: http://name1.domain1.com/ -> index.php?subdomain=name1 http://name2.domain2.org/ -> index.php?subdomain=name2 我需要在php脚本(index.php)中管理子域GET变量。 我需要所有域的通用规则 这是我的.htaccess文件,但它不工作 RewriteEngine On # Parse the subdomain a

我可以重写此请求类型:

http://name1.domain1.com/        -> index.php?subdomain=name1
http://name2.domain2.org/        -> index.php?subdomain=name2
我需要在php脚本(index.php)中管理子域GET变量。 我需要所有域的通用规则

这是我的.htaccess文件,但它不工作

RewriteEngine On

# Parse the subdomain as a variable we can access in PHP, and
# run the main index.php script
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST}        !^www
RewriteCond %{HTTP_HOST}         ^([^\.]+)\.([^\.]+)\.([^\.]+)$
RewriteRule ^(.*)$ /$1?subdomain=%1

谢谢。

假设您使用的是PHP。您不需要将其作为查询参数,因为从PHP您已经可以看到客户端浏览器请求的URL

echo $_SERVER['HTTP_HOST']
输出:

bubbles.domain.com
bubbles
你只需要解析它

$host= (explode( '.', $_SERVER['HTTP_HOST']))[0];
$subdomain = $host[0];

if($subdomain == 'www' || $subdomain == 'www2') 
    $subdomain = $host[1];

echo $subdomain;
输出:

bubbles.domain.com
bubbles

假设您使用的是PHP。您不需要将其作为查询参数,因为从PHP您已经可以看到客户端浏览器请求的URL

echo $_SERVER['HTTP_HOST']
输出:

bubbles.domain.com
bubbles
你只需要解析它

$host= (explode( '.', $_SERVER['HTTP_HOST']))[0];
$subdomain = $host[0];

if($subdomain == 'www' || $subdomain == 'www2') 
    $subdomain = $host[1];

echo $subdomain;
输出:

bubbles.domain.com
bubbles
如果设置了“www”前缀:$host=explode('.',$服务器['HTTP\u主机']);如果(count($host)==3){//esiste-il-www$subdomain=$host[1];}elseif($host==2){//non-esiste-www$subdomain=$host[0];}您同意吗?如果设置了“www”前缀:$host=explode('.',$u服务器['HTTP_-host']);如果(count($host)==3){//esiste il www$subdomain=$host[1];}elseif($host==2){//non-esiste www$subdomain=$host[0];}您同意吗?