将子域添加到本地主机 URL [英] Add subdomain to localhost URL

查看:36
本文介绍了将子域添加到本地主机 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Web 应用程序,该应用程序的行为取决于 url 前缀.格式类似于:

I am writing an web application that behaves differently depending on a url prefix. The format is something like:

   https://myprefix.mycompany.com

Web 应用程序的行为因 myprefix 而异.我的网络应用程序从 URL 中提取该部分并对其进行操作.

The web app behaves differently based on myprefix. My web app extract that part from the URL and act on that.

但是,当我在本地进行测试时,我使用的是本地主机地址:

However, when I test on my local, I use an localhost address:

   https://localhost:1234

我不能做这样的事情:

   https://myprefix.localhost:1234

测试这个场景的最佳方式是什么?

What is the best way for me to test this scenario?

非常感谢

推荐答案

不幸的是,因为 localhost 不是一个合适的域,你不能像那样向它添加子域.但是,您可以欺骗您的计算机,使其认为它拥有一个特定的域并以此方式进行测试.例如,如果您有一个基于 UNIX 的操作系统,请(以 root 身份)打开文件 /etc/hosts 并添加如下一行(或多行):

Unfortunately, because localhost is not a proper domain, you can't add a subdomain to it like that. You can, however, trick your computer into thinking it owns a specific domain and test things that way. For instance, if you have a UNIX-based operating system, open (as root) the file /etc/hosts and add a line (or lines) like this:

127.0.0.1    example.com
127.0.0.1    subdomain.example.com

您的计算机现在会将 example.comsubdomain.example.com 视为属于自己的.如果您在 Web 浏览器中访问其中任何一个,它们的工作原理与 localhost 相同,但您的 Web 服务器将在其主机标头中看到正确的域.

Your computer will now treat both example.com and subdomain.example.com as belonging to itself. If you visit either in your web browser, they will work the same, in principle, as localhost, but your web server will see the correct domain in its Host header.

这篇关于将子域添加到本地主机 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆