php WAMP - 从项目 URL 中删除 localhost

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23551763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:48:08  来源:igfitidea点击:

WAMP - Remove localhost from project URL

phpapacheurllocalhostwamp

提问by Rafael de Castro

The URLs of my projects in WAMP are not resolving as I'd expect. For example, I'd expect the project in the folder c:\wamp\www\project1 to have the URL http://project1/, but it actually has the URL http://localhost/project1/.

我在 WAMP 中的项目的 URL 没有像我期望的那样解析。例如,我希望文件夹 c:\wamp\www\project1 中的项目具有 URL http://project1/,但它实际上具有 URL http://localhost/project1/

This can cause problems when accessing server variables. How do I fix this?

这会在访问服务器变量时导致问题。我该如何解决?

回答by RiggsFolly

You can also look at this answerspecially if you are now using WAMPServer 3or greater, for a simple clikc and go way to create Virtual hosts.

如果您现在使用WAMPServer 3或更高版本,您还可以专门查看此答案,以通过简单的 clikc 和 go 方式创建虚拟主机。

Actually this change was intended by the WAMPServer developers and for a good reason.

实际上,此更改是 WAMPServer 开发人员有意设计的,并且是有充分理由的。

There is a problem using the localhost/project1url and the default WAMPServer DocumentRoot in that it causes problems for some frameworks and WordPress type environments, as well as your own code if you are using code which depends on knowing anything about the server environment.

使用localhost/project1url 和默认 WAMPServer DocumentRoot 存在问题,因为它会导致某些框架和 WordPress 类型环境出现问题,如果您使用的代码依赖于对服务器环境的了解,则会导致您自己的代码出现问题。

The correct solution is to create Virtual Hosts for all your projects even those that you store in the \wamp\www\project1style folders.

正确的解决方案是为所有项目创建虚拟主机,甚至是存储在\wamp\www\project1样式文件夹中的项目。

When doing that the DocumentRoot is \wamp\wwwand that is what causes these problems.

这样做时 DocumentRoot 是\wamp\www,这就是导致这些问题的原因。

These tools expect the DocumentRoot to be the root of the site i.e. \wamp\www\project1so that when they use PHP variables like

这些工具期望 DocumentRoot 是站点的根目录,即\wamp\www\project1,当它们使用 PHP 变量时,例如

$_SERVER['HTTP_HOST']
$_SERVER['SERVER_NAME']
$_SERVER['DOCUMENT_ROOT']

they get the correct answer i.e. the answer they would get on a real live server hosting just that site.

他们会得到正确的答案,即他们会在仅托管该站点的真实实时服务器上得到的答案。

So using the localhost\project1style url would mean these variables would return

因此,使用localhost\project1样式 url 将意味着这些变量将返回

$_SERVER['HTTP_HOST'] = localhost
$_SERVER['SERVER_NAME'] = localhost
$_SERVER['DOCUMENT_ROOT'] = C:/wamp/www

When they should return

他们应该什么时候回来

$_SERVER['HTTP_HOST'] = project1
$_SERVER['SERVER_NAME'] = project1
$_SERVER['DOCUMENT_ROOT'] = C:/wamp/www/project1

So what you should do to make the My Projectsmenu work and reduce your pain in copying sites to live servers is:

因此,您应该做的是使My Projects菜单工作并减少将站点复制到实时服务器的痛苦:

Create an entry in the HOSTS file for each project like so and remember to create one for access via IPV4(127.0.0.1) and one for access via IPV6 (::1):-

像这样在 HOSTS 文件中为每个项目创建一个条目,并记住创建一个用于通过 IPV4(127.0.0.1) 访问和一个用于通过 IPV6 (::1) 访问:-

127.0.0.1 localhost
127.0.0.1 project1

::1 localhost
::1 project1

Remember to refresh the Windows DNS Cache after any change to this file like so :-

请记住在对此文件进行任何更改后刷新 Windows DNS 缓存,如下所示:-

Start a command window using Run as Administratorand run :-

使用启动命令窗口Run as Administrator并运行:-

net stop Dnscache
net start Dnscache

Now you must create a Virtual Host definition, so edit the \wamp\bin\apache\apache2.4.9\conf\extra\httpd-vhost.conffile ( apache versions may differ )

现在您必须创建一个虚拟主机定义,因此编辑\wamp\bin\apache\apache2.4.9\conf\extra\httpd-vhost.conf文件(apache 版本可能不同)

Delete the default stuff in there the first time you do this. And then create your Virtual Host definitions like so :-

第一次执行此操作时,请删除其中的默认内容。然后像这样创建您的虚拟主机定义:-

#
# Use name-based virtual hosting.
# This next line is not required if you are using Apache 2.4.x and should be deleted
NameVirtualHost *:80

## should be first so the wamp menu page loads and is the default site
## should also never be changed from only allowing access from the local machine
## for a bit of extra security from casual ip address probing
<VirtualHost *:80>
    DocumentRoot "C:/wamp/www"
    ServerName  localhost
    ServerAlias localhost
    <Directory  "C:/wamp/www">
        AllowOverride All

        <IfDefine APACHE24>
            Require local
        </IfDefine>

        <IfDefine !APACHE24>
            Order Deny,Allow
            Deny from all
            Allow from 127.0.0.1 localhost ::1
        </IfDefine>
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "C:/wamp/www/project1"
    ServerName  project1
    ServerAlias project1
    <Directory  "C:/wamp/www/project1">
        AllowOverride All

        <IfDefine APACHE24>
            Require local
        </IfDefine>

        <IfDefine !APACHE24>
            Order Deny,Allow
            Deny from all
            Allow from 127.0.0.1 localhost ::1
        </IfDefine>

    </Directory>
</VirtualHost>

Now you need one more change, you must uncomment the line in httpd.confthat includes the above file we have just changed. So edit the httpd.conffile, use the wampmanager menus to do this as it ensures you edit the correct file.

现在您需要再进行一项更改,您必须取消注释httpd.conf包含我们刚刚更改的上述文件的行。因此,请编辑httpd.conf文件,使用 wampmanager 菜单来执行此操作,因为它可以确保您编辑正确的文件。

Find this line #Include conf/extra/httpd-vhosts.confand remove the comment #symbol from the beginning of the line like so :-

找到这一行#Include conf/extra/httpd-vhosts.conf#从该行的开头删除注释符号,如下所示:-

Include conf/extra/httpd-vhosts.conf

Now of course you will need to restart Apache so that it picks up your configuration changes.

当然,现在您需要重新启动 Apache,以便它接收您的配置更改。

If Apache does not restart, you probably made a mistake in the config, to find out what is wrong try this.

如果 Apache 没有重新启动,您可能在配置中犯了一个错误,要找出问题所在,试试这个。

Open a command window and CDinto the \wamp\bin\apache\apache2.4.9\binfolder.

打开命令窗口并CD进入\wamp\bin\apache\apache2.4.9\bin文件夹。

Then run this :-

然后运行这个:-

httpd -t

If the error is in httpd.confor the httpd-vhost.conffiles it will tell you the error and also give you the line number to make finding the error very easy.

如果错误在httpd.confhttpd-vhost.conf文件中,它会告诉您错误并为您提供行号,以便轻松查找错误。

回答by user3795145

in your www folder open index.php at line 30 change: $suppress_localhost to be false

在您的 www 文件夹中打开 index.php 在第 30 行更改: $suppress_localhost 为 false

this is should look:

这应该看起来:

$suppress_localhost = false;

$suppress_localhost = false;

回答by pud

In your wwwfolder open index.phpat line 30. Here, change $suppress_localhostto be false. So, it is should look:

在您的www文件夹中打开index.php第 30 行。在这里,更改$suppress_localhostfalse。所以,它应该看起来:

$suppress_localhost = false;

That was the quickest and easiest fix for me. I'm using 64 bit Wamp.

这对我来说是最快和最简单的解决方法。我正在使用 64 位 Wamp。

回答by ejbytes

Okay, I had this problem. So, I troubleshooted the problem and traced it to an actual solution, NOT A HACK.

好的,我遇到了这个问题。因此,我解决了问题并将其追溯到实际解决方案,而不是 HACK。

The SOLUTION:

解决方案

  1. Right-Click WAMP-server icon
  2. Select WAMP Settings,
  3. Select (enable) option Add localhost in URL
  1. 右键单击 WAMP 服务器图标
  2. 选择WAMP Settings,
  3. 选择(启用)选项 Add localhost in URL


DONE. The remainder is FYI of how and why.


完成。其余的是关于如何以及为什么的仅供参考。



Note: localhost/myproject.php or myproject.php. Although the solution was already accepted, I saw some posts that got me confused. The accepted solution is based on a single project wrt different server applications, based on the OP's specific question, and how to influence that single project in question. But all the other solutions are hacks and don't really answer the solution to the OP's question, but do bring up a good point about the URL. So, according to the other "solutions", here is how to toggle the localhost reference in the URL. Hence my additional solution added to the mix.

注意:localhost/myproject.php 或 myproject.php。虽然解决方案已经被接受,但我看到了一些让我感到困惑的帖子。接受的解决方案基于与不同服务器应用程序的单个项目,基于 OP 的特定问题,以及如何影响有问题的单个项目。但是所有其他解决方案都是黑客,并没有真正回答 OP 问题的解决方案,但确实提出了有关 URL 的好观点。因此,根据其他“解决方案”,这里是如何切换 URL 中的 localhost 引用。因此,我的额外解决方案添加到混合中。



This is a toggle switch.


Troubleshooting Process (no hacking involved):

故障排除过程(不涉及黑客攻击):




Let's peek at the index.php


让我们看看index.php

enter image description here

在此处输入图片说明



Let's look at the config file. Note the variables and Array?

我们来看看配置文件。注意变量和数组?

enter image description here

在此处输入图片说明



Here is the array. A variable used earlier. Let's see... Oh, it tells us where and what to do.

这是数组。之前使用的变量。让我们看看... 哦,它告诉我们在哪里做什么,做什么。

enter image description here

在此处输入图片说明



As noted in the SOLUTION:

解决方案中所述:

enter image description here

在此处输入图片说明

回答by nl-x

Your wamp seems to be configured to run a website on the normally non-existant domain helloworld.

您的 wamp 似乎已配置为在通常不存在的域 helloworld 上运行网站。

add:

添加:

 127.0.0.1    helloworld

inside this file: c:\windows\system32\drivers\etc\hosts

在这个文件中: c:\windows\system32\drivers\etc\hosts

Make sure you start your text editor with administrator privileges to be able to edit that file.

确保以管理员权限启动文本编辑器,以便能够编辑该文件。

This will tell your computer that the otherwise non-existant domain helloworldshould be resolved to your loop back address.

这将告诉您的计算机,否则不存在的域helloworld应解析为您的环回地址。

回答by Jaroslav Klim?ík

For me was the easiest way go to http://localhostand in wampserver homepage use Add a Virtual Host(Tools section). There is nice and easy form to create alias without any problem (instead console when you using tray icon to create alias). No source edit, just using what wamp offers. Remember refresh DNS after creating of alias. Tested on Win10, WampServer 3.0.6 64bit.

对我来说,最简单的方法是访问http://localhost并在 wampserver 主页中使用Add a Virtual Host(工具部分)。有一个很好且简单的形式来创建别名,没有任何问题(当您使用托盘图标创建别名时,而不是控制台)。没有源代码编辑,只需使用 wamp 提供的内容。创建别名后记得刷新DNS。在 Win10、WampServer 3.0.6 64 位上测试。

回答by arrowgr

C:\wamp\www

In index.php 
line 338 

($suppress_localhost ? 'http://' : '')


change http://   to http://localhost/

回答by Rakesh

To do this you can create a virtual hostusing Add a virtual Hostutility under Toolsmenu on localhost's homepage.
For more info on how to create a virtual host visit : Step by step instructions

为此,您可以使用本地主机主页上工具菜单下的实用程序创建虚拟主机。 有关如何创建虚拟主机的更多信息,请访问:分步说明Add a virtual Host

回答by Ole

I think that the easiest and quickest way is to:

我认为最简单快捷的方法是:

Open index.php in your www folder >>> change: $suppress_localhost to be false / no.

在您的 www 文件夹中打开 index.php >>> 更改: $suppress_localhost 为 false / no。

回答by Jim

This isn't really an answer per se. It seems that the quickest way of removing a Virtual Host using WAMP is either not create one in the first place or be prepared to uninstall/reinstall it. What is the path to the config file to correct a errant and otherwise not malfunctioning WAMP Server?

这本身并不是一个真正的答案。似乎使用 WAMP 删除虚拟主机的最快方法不是一开始就创建一个,或者准备卸载/重新安装它。配置文件的路径是什么来纠正错误的 WAMP 服务器,否则不会出现故障?