dseax40600
dseax40600
2017-03-30 12:27

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

已采纳

Ho I Can rewrite this request types:

http://name1.domain1.com/        -> index.php?subdomain=name1
http://name2.domain2.org/        -> index.php?subdomain=name2

I need manage subdomain GET variable in php script ( index.php ). I need of generic rule for all domain.

This is my .htaccess file, but it's not working.

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

Thanks.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • doutai1509 doutai1509 4年前

    Assuming you are using PHP. You don't need to put it as query parameter since from PHP you can already see which is the URL the client's browser requested.

    echo $_SERVER['HTTP_HOST']
    

    output:

    bubbles.domain.com
    

    You just need to parse it.

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

    output:

    bubbles
    
    点赞 评论 复制链接分享

相关推荐