如何区分环境是否是虚拟主机?
我这里有这段代码:
$config['SUBFOLDER'] = '/';
$config['APP_URL'] = 'http://'.$_SERVER['HTTP_HOST'].$config['SUBFOLDER'];
APP_URL 在整个 HTML 模板中使用。问题是 - 配置需要尽可能通用,因此切换环境时要做的事情较少。
现在,当我为我的项目配置虚拟主机时,它的工作原理是这样的,但是当它不是虚拟主机,而是某种 localhost/myproject/ 时 - $config['SUBFOLDER'] 必须手动设置为/myproject/
如何以编程方式执行此操作?
I have this code here:
$config['SUBFOLDER'] = '/';
$config['APP_URL'] = 'http://'.$_SERVER['HTTP_HOST'].$config['SUBFOLDER'];
And APP_URL is used throughout the HTML templates. The problem is - the config needs to be as universal as possible, so there's less to do when switching environments.
Now, it works like this when I have configured a virtual host for my project, but when it isn't a virtual host, but sort of a localhost/myproject/ - the $config['SUBFOLDER'] has to be set manually to /myproject/
How do I do this programmatically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
例如,您可以在 htaccess 文件中设置一个变量。
然后在您的网站中对此进行检查:
使用这种方法,您只需更改 htaccess 文件中的一个变量。
如果您只想检查是否在本地主机上运行,则检查
$_SERVER['HTTP_HOST']
也可以:You could set a variable in, for example, your htaccess file.
then in your website do a check for this:
With this approach you only need to change one variable in the htaccess file.
If you just want to check if you are running on localhost, doing a check on
$_SERVER['HTTP_HOST']
will also work:检查 URL 是否包含 LOCALHOST 还是普通域名,并将您的配置包装在依赖于 URL 的条件中。
Check the URL whether it contains LOCALHOST or a normal domain name and wrap your config up in a conditional dependent on what the URL is.
我采取稍微不同的方法..
I take a slightly different approach..