dseax40600 2017-03-30 12:27
浏览 61
已采纳

.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 2017-03-30 12:59
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊