检索子域作为 get 变量

发布于 2024-09-09 21:41:57 字数 191 浏览 6 评论 0原文

大家好,我正在设置 mywebapplication 为用户提供唯一的 url,例如 uniquename.mysite.com、anotheuniqname.mysite.com 等。

我希望能够在 php 脚本中获取 url 的子域部分。如果我可以将它作为 GET 变量获取,那就太好了 - 我认为可以使用 htaccess 来完成。有什么想法吗?

Hi guys I'm setting up mywebapplication to give unique urls for users such as uniquename.mysite.com, anotheuniqname.mysite.com etc.

I want to be able to in a php script grab the subdomain part of the url. Would be nice if I can get it as a GET variable - I think it can be done with htaccess. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

缱绻入梦 2024-09-16 21:41:57
$subdomain = substr($_SERVER['HTTP_HOST'], 0, strpos($_SERVER['HTTP_HOST'], '.'));

要确保存在有效的子域:

$urlExplode = explode('.', $_SERVER['HTTP_HOST']);

if (count($urlExplode) > 2 && $urlExplode[0] !== 'www') {

    $subdomain = $urlExplode[0];

}
else {
    // treat as www.mysite.com or mysite.com
}
$subdomain = substr($_SERVER['HTTP_HOST'], 0, strpos($_SERVER['HTTP_HOST'], '.'));

To make sure there's a valid subdomain:

$urlExplode = explode('.', $_SERVER['HTTP_HOST']);

if (count($urlExplode) > 2 && $urlExplode[0] !== 'www') {

    $subdomain = $urlExplode[0];

}
else {
    // treat as www.mysite.com or mysite.com
}
夕色琉璃 2024-09-16 21:41:57

我会尝试这个(在 PHP 代码的开头):

$_GET['subdomain'] = substr($_SERVER['SERVER_NAME'], 0, strrpos($_SERVER['SERVER_NAME'], '.'));

解释:strrpos 查找最后一次出现的 '.'在访问的域中,substr 返回到该位置的字符串。我将返回值分配给 $_GET var,这样您就可以像普通 URL 参数一样使用它。

I would try this (at the beginning of your PHP code):

$_GET['subdomain'] = substr($_SERVER['SERVER_NAME'], 0, strrpos($_SERVER['SERVER_NAME'], '.'));

Explanation: strrpos find the last occurance of '.' in the accessed domain, substr then returns the string up to this position. I assigned the return value to a $_GET var so you can use it as if it were a normal URL-parameter.

孤者何惧 2024-09-16 21:41:57

将其放入您的引导文件中:

$tmp = explode(".", $_SERVER["HTTP_HOST"], 2);
$_GET["subdomain"] = $tmp[0];

Put this in your bootstrap file:

$tmp = explode(".", $_SERVER["HTTP_HOST"], 2);
$_GET["subdomain"] = $tmp[0];
甜尕妞 2024-09-16 21:41:57

Ali - 您不是说 $_SERVER 变量因为 $_GET 与查询字符串相关吗?

如果是这样,那么你可以尝试:

$_SERVER['HTTP_HOST'] 

jim

[edit] - 请参阅 http://roshanbh.com.np/2008/05/useful-server-variables-php.html 一些可能有帮助的示例

Ali - don't you mean a $_SERVER variable as $_GET would be related to the querystring ??

if so, then you could try:

$_SERVER['HTTP_HOST'] 

jim

[edit] - see http://roshanbh.com.np/2008/05/useful-server-variables-php.html for a few examples that may help

逆流 2024-09-16 21:41:57

您想要以下内容吗:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^[a-z0-9-]+\.yoursite\.com$
RewriteRule !^index\.php$ index.php [L]

然后您可以使用以下方式获取子域

<?php

preg_match('/^http:\/\/([a-z0-9-]+)\.yoursite.com
, $_SERVER['HTTP_HOST'], $matches);

$_GET['username'] = $matches[1];

?>

Do you want something along the lines of:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^[a-z0-9-]+\.yoursite\.com$
RewriteRule !^index\.php$ index.php [L]

you could then get the subdomain using

<?php

preg_match('/^http:\/\/([a-z0-9-]+)\.yoursite.com
, $_SERVER['HTTP_HOST'], $matches);

$_GET['username'] = $matches[1];

?>
鹿童谣 2024-09-16 21:41:57

如果您确实想在 .htaccess 文件中使用 mod_rewrite,您可以执行类似的操作(假设您的脚本是 index.php例子):

RewriteEngine On

# Prevent people from going to to index.php directly and spoofing the
# subdomain value, since this may not be a desirable action
RewriteCond %{THE_REQUEST} ^[A-Z]+\sindex\.php.*(\?|&)subdomain=
RewriteRule ^.*$ http://%{HTTP_HOST}/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST}        !^www
RewriteCond %{HTTP_HOST}         ^([^\.]+)\.([^\.]+)\.([^\.]+)$
RewriteRule ^.*$ index.php?subdomain=%1

If you really want to use mod_rewrite in your .htaccess file, you could do something like this (assuming your script is index.php in this example):

RewriteEngine On

# Prevent people from going to to index.php directly and spoofing the
# subdomain value, since this may not be a desirable action
RewriteCond %{THE_REQUEST} ^[A-Z]+\sindex\.php.*(\?|&)subdomain=
RewriteRule ^.*$ http://%{HTTP_HOST}/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST}        !^www
RewriteCond %{HTTP_HOST}         ^([^\.]+)\.([^\.]+)\.([^\.]+)$
RewriteRule ^.*$ index.php?subdomain=%1
牵你的手,一向走下去 2024-09-16 21:41:57

这个小函数将简单地获取您的“ www.website.com ”或您的情况下的“ subdomain.website.com ”,然后将其分成三部分( 0=>"www", 1=>"website", 2=>"com" ),然后返回值 1,即“ www " 或您的子域名。

function isSubdomain()
{
    $host = $_SERVER['HTTP_HOST'];
    $host = explode('.',$host);
    $host = (is_array($host)?$host[0]:$host);
    return $host;
}

示例:如果您的网站是“ admin.website.com ”,您将收到“admin”。

This little function will simply get your " www.website.com " or in your case " subdomain.website.com ", and then split it up in 3 parts ( 0=>"www", 1=>"website", 2=>"com" ), and then return value 1, which is either " www " or your subdomain name.

function isSubdomain()
{
    $host = $_SERVER['HTTP_HOST'];
    $host = explode('.',$host);
    $host = (is_array($host)?$host[0]:$host);
    return $host;
}

Exaple: If your website is " admin.website.com ", you will receive "admin".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文