将子域添加到localhost URL [英] Add subdomain to localhost 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
基于myprefix,Web应用程序的行为有所不同。我的网络应用程序从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.com
和子域。 example.com
属于自己。如果您在Web浏览器中访问它们,原则上它们的工作方式与 localhost
相同,但您的Web服务器将在其Host标头中看到正确的域。
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.
这篇关于将子域添加到localhost URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!