【问题标题】:.htaccess rewrite subdomain as request parameter.htaccess 重写子域作为请求参数
【发布时间】:2017-03-30 12:27:25
【问题描述】:

我可以重写这个请求类型:

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 wildcard subdomain


    【解决方案1】:

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

    echo $_SERVER['HTTP_HOST']
    

    输出:

    bubbles.domain.com
    

    你只需要解析它。

    $host= (explode( '.', $_SERVER['HTTP_HOST']))[0];
    $subdomain = $host[0];
    
    if($subdomain == 'www' || $subdomain == 'www2') 
        $subdomain = $host[1];
    
    echo $subdomain;
    

    输出:

    bubbles
    

    【讨论】:

    • 如果 isset "www" 前缀:$host = explode('.',$_SERVER['HTTP_HOST'])); if(count($host) == 3) { // esste il www $subdomain = $host[1]; } elseif($host == 2) { // 非 eseste www $subdomain = $host[0]; } 你同意吗?
    猜你喜欢
    • 2010-10-31
    • 2016-01-30
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    • 2011-02-04
    • 2014-03-17
    • 1970-01-01
    相关资源
    最近更新 更多