检索子域作为 get 变量
大家好,我正在设置 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
要确保存在有效的子域:
To make sure there's a valid subdomain:
我会尝试这个(在 PHP 代码的开头):
解释:
strrpos
查找最后一次出现的 '.'在访问的域中,substr
返回到该位置的字符串。我将返回值分配给$_GET
var,这样您就可以像普通 URL 参数一样使用它。I would try this (at the beginning of your PHP code):
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.将其放入您的引导文件中:
Put this in your bootstrap file:
Ali - 您不是说
$_SERVER
变量因为$_GET
与查询字符串相关吗?如果是这样,那么你可以尝试:
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:
jim
[edit] - see http://roshanbh.com.np/2008/05/useful-server-variables-php.html for a few examples that may help
您想要以下内容吗:
然后您可以使用以下方式获取子域
Do you want something along the lines of:
you could then get the subdomain using
如果您确实想在
.htaccess
文件中使用mod_rewrite
,您可以执行类似的操作(假设您的脚本是index.php
例子):If you really want to use
mod_rewrite
in your.htaccess
file, you could do something like this (assuming your script isindex.php
in this example):这个小函数将简单地获取您的“
www.website.com
”或您的情况下的“subdomain.website.com
”,然后将其分成三部分( 0=>"www", 1=>"website", 2=>"com" )
,然后返回值1
,即“www
" 或您的子域名。示例:如果您的网站是“
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 value1
, which is either "www
" or your subdomain name.Exaple: If your website is "
admin.website.com
", you will receive "admin
".