如何为具有相同 IP 的多个域/主机设置主机文件? [英] How do I set up the hosts file for multiple domains/hosts with the same IP?

查看:80
本文介绍了如何为具有相同 IP 的多个域/主机设置主机文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您肯定知道,您可以使用hosts"文件将主机名映射到 IP 地址.这非常有用,尤其是在您进行开发时,因为您可以更改 project1 的 localhost.

As you surely know you can map host names to IP addresses with the "hosts" file. This is very useful especially when you are developing as you can change localhost for project1.

那部分很清楚,但不幸的是我不知道如何设置它以打开具有相同 IP 的多个项目.我尝试了一些没有成功的事情,例如:

That part is clear, but unfortunately I can't figure out how to set this up to open multiple projects with the same IP. I've tried a few things without success such as:

127.0.0.1/projects/project1  project1
127.0.0.1/projects/project2  project2

我查了一些相关的问题,但找不到答案.那么,我能以某种方式完成我想要做的事情吗?有什么建议吗?

I checked some related questions but I couldn't find an answer. So, can I accomplish what I'm trying to do somehow? Any suggestions?

推荐答案

感谢 Google 和合作者 @jvilhena 和 @DRC,我解决了这个问题.我是这样做的:

I got this resolved thanks to Google and the collaborators, @jvilhena and @DRC. Here's how I did it:

如果您像我一样使用 Windows 和 XAMPP,第一步是设置hosts"文件.如果您使用的是 Windows,您很可能会在 C:\Windows\System32\drivers\etc\hosts 中找到它.您可以使用任何文本编辑器对其进行编辑.

If you are using Windows and XAMPP as in my case the first step is to set up the 'hosts' file. If you are using Windows it's likely that you will find it in C:\Windows\System32\drivers\etc\hosts. You can use any text editor to edit it.

您可以设置任意数量的主机名,全部指向您的本地主机,IP 为 127.0.0.1.

You can set up as many host names as you like all pointing to your localhost, with the IP, 127.0.0.1.

例如:

 127.0.0.1               local.project1
 127.0.0.1               local.project2
 127.0.0.1               youcanuseany.name.here

第二步是处理Apache文件httpd-vhosts.conf.同样,我使用的是 Windows 和 XAMPP.该文件很可能位于 C:\xampp\apache\conf\extra\httpd-vhosts.conf.

The second step was to deal with the Apache file httpd-vhosts.conf. Again, I'm using Windows and XAMPP. It's likely this file will be in C:\xampp\apache\conf\extra\httpd-vhosts.conf.

您不必这样做,但我喜欢将我的项目文件夹保存在我的 htdocs 文件夹 @ C:\xampp\htdocs 中.

You don't have to but I like to keep my project folders in my htdocs folder @ C:\xampp\htdocs.

对于您为其创建主机名"的每个项目,您应该将以下内容添加到您的 httpd-vhosts.conf 文件中:

For each project that you create a "host name" for, you should add the following to your httpd-vhosts.conf file:

<VirtualHost *>
    DocumentRoot "C:\xampp\htdocs\projectx"
    ServerName youcanuseany.name.here
    <Directory "C:\xampp\htdocs\projectx">
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>  

再举一个例子:)

<VirtualHost *>
    DocumentRoot "C:\xampp\htdocs\project1"
    ServerName local.project1
    <Directory "C:\xampp\htdocs\project1">
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

现在您可以在浏览器中输入 local.project1youcanuseany.name.here,它应该会像输入 localhost/project1 一样打开您的项目localhost/projectX.我希望这会有所帮助.

Now you can type local.project1 and youcanuseany.name.here in your browser and it should open your project as if you were typing localhost/project1 and localhost/projectX. I hope this helps.

这篇关于如何为具有相同 IP 的多个域/主机设置主机文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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