php 在 xampp 中使用带有 https 的域名而不是本地主机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16229126/
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 10:44:51  来源:igfitidea点击:

Using Domain name instead of localhost in with https in xampp

phpapachexampplocalhostdomain-name

提问by Jeet Chaudhari

My question may be stupid, But honestly I searched a lot and got success but not complete.

我的问题可能很愚蠢,但老实说,我搜索了很多并取得了成功,但还没有完成。

I use xampp with windows 8.

我在 Windows 8 中使用 xampp。

My host file looks as follows.

我的主机文件如下所示。

    127.0.0.1   localhost
    127.0.0.1   www.mysite.com

My httpd-vhosts.config looks as follows.

我的 httpd-vhosts.config 如下所示。

    NameVirtualHost 127.0.0.1
    <VirtualHost 127.0.0.1>
        DocumentRoot "C:/xampp/htdocs"
        ServerName localhost
    </VirtualHost>
    <VirtualHost 127.0.0.1>
        ServerName www.mysite.com
        ServerAlias mysite.com
        DocumentRoot "C:/xampp/htdocs/mysite"
    </VirtualHost>

This works perfect for http. But I have enabled ssl.

这适用于http。但是我启用了ssl。

When I type http://localhostor https://localhost, Both work fine.

当我输入http://localhostor 时https://localhost,两者都可以正常工作。

When I type http://mysite.comit works,

当我输入http://mysite.com它时,

when I type https://mysite.comit is redirected as https://mysite.com/xampp/and shows me default welcome page of xampp.

当我输入时,https://mysite.com它被重定向为https://mysite.com/xampp/并显示 xampp 的默认欢迎页面。

I tried following things.

我试过以下事情。

1) instead of using 127.0.0.1, I tried using *:80 in httpd-vhosts.conf But result was same.

1) 而不是使用 127.0.0.1,我尝试在 httpd-vhosts.conf 中使用 *:80 但结果是一样的。

2) instead of using 127.0.0.1, I tried using *:443 in httpd-vhosts.conf But at the time of restarting apache fails to start again.

2) 我尝试在 httpd-vhosts.conf 中使用 *:443 而不是使用 127.0.0.1 但在重新启动 apache 时无法再次启动。

Please let me know how can I access my site through domain name instead of localhost with https or http.

请让我知道如何通过域名而不是带有 https 或 http 的 localhost 访问我的网站。

采纳答案by Jeet Chaudhari

I tried many things, But I think I missed basic edit.

我尝试了很多东西,但我想我错过了基本的编辑。

Now all working fine.

现在一切正常。

Now host file is still the same as mentioned in question. I did not make any changes to it.

现在主机文件仍然与问题中提到的相同。我没有对其进行任何更改。

I changed port in httpd-vhosts.config as shows below.

我更改了 httpd-vhosts.config 中的端口,如下所示。

NameVirtualHost *
    <VirtualHost *>
        DocumentRoot "C:/xampp/htdocs"
        ServerName localhost
    </VirtualHost>
    <VirtualHost *>
        ServerName www.mysite.com
        ServerAlias mysite.com
        DocumentRoot "C:/xampp/htdocs/mysite"
    </VirtualHost>

Also the step I missed, was editing httpd-ssl.config file in same folder that of httpd-vhosts.config.

同样我错过的步骤是在 httpd-vhosts.config 的同一文件夹中编辑 httpd-ssl.config 文件。

I just added following lines before last line of http-ssl.config file i.e. < /IfModule>

我只是在 http-ssl.config 文件的最后一行之前添加了以下几行,即 < /IfModule>

<VirtualHost _default_:443> 
    DocumentRoot "C:/xampp/htdocs/mysite" 
    ServerName www.mysite.com:443 
    ServerAlias mysite.com:443  
    SSLEngine on 
    SSLCertificateFile "conf/ssl.crt/server.crt" 
    SSLCertificateKeyFile "conf/ssl.key/server.key" 
</VirtualHost> 

Thank You all friends for helping me lot on this, Without your links I would never ever be able to find out that I need to edit one more file.

感谢所有在这方面帮助我的朋友,如果没有您的链接,我将永远无法发现我需要再编辑一个文件。

回答by Anurag Prashant

Let me explain step by step for other guys too.

让我也为其他人一步一步地解释。

1. Map your custom domain name to localhost in HOSTS file.

1. 将您的自定义域名映射到 HOSTS 文件中的 localhost。

Open hosts file and add below line.

打开主机文件并添加以下行。

127.0.0.1 www.example.com


2.Tell XAMPP about your custom domain.

2.将您的自定义域告诉 XAMPP。

Add below content to httpd-vhosts.conf

将以下内容添加到httpd-vhosts.conf

<VirtualHost *>
    ServerName www.example.com
    ServerAlias example.com
    DocumentRoot "C:/xampp/htdocs/example"
</VirtualHost>

If you have port for your localhost, then add it as <VirtualHost *:80>

如果您有本地主机的端口,则将其添加为 <VirtualHost *:80>

Restart apache,Now you can access http://example.comin your browser.

重启apache,现在你可以在浏览器中访问http://example.com

3. If you want to access https://example.com

3.如果你想访问https://example.com

Add below line to httpd-vhosts.conf

将以下行添加到httpd-vhosts.conf

<VirtualHost *:443>
    DocumentRoot "C:/xampp/htdocs/example"
    ServerName www.example.com
    SSLEngine on
    SSLCertificateFile "conf/ssl.crt/server.crt"
    SSLCertificateKeyFile "conf/ssl.key/server.key"
    <Directory "C:/xampp/htdocs/example">
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>


回答by Xman Classical

I started with multiple custom domains. See new code below:

我从多个自定义域开始。请参阅下面的新代码:

Note: WordPress strips backslashes, so below I've replaced them with forward slashes. I believe it with work regardless either way.

注意:WordPress 去掉了反斜杠,所以下面我用正斜杠替换了它们。无论哪种方式,我都相信工作。

<VirtualHost *:80>
    DocumentRoot "C:/xampp/htdocs"
    ServerName localhost
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "C:/Users/Austin Passy/Documents/InMotion Hosting/frostywebdev.com/html"
    ServerName frostyweb.dev
    <Directory "C:/Users/Austin Passy/Documents/InMotion Hosting/frostywebdev.com/html">
    Options Indexes FollowSymLinks ExecCGI Includes
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "C:/xampp/htdocs/eateryengine"
    ServerName eateryengine.dev
    <Directory "C:/xampp/htdocs/eateryengine">
    Options Indexes FollowSymLinks ExecCGI Includes
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

回答by FaTe

I have been googling for hours trying to figure out why the newest XAMPP release puts 1200MS on page generation times... I thought it was maybe my code working with some pretty complex class systems yet.. this thread pointed out the whole localhost <> 127.0.0.1

我一直在谷歌搜索几个小时试图弄清楚为什么最新的 XAMPP 版本将 1200MS 放在页面生成时间上......我认为这可能是我的代码与一些非常复杂的类系统一起工作......这个线程指出了整个 localhost <> 127.0.0.1

I'm on Windows 7 and I didn't think to use CMD to "ping localhost"

我在 Windows 7 上,我没想到使用 CMD 来“ping localhost”

the result was "::1:" not 127.0.0.1

结果是 "::1:" 不是 127.0.0.1

After a quick windows/system32/drivers/etc/host file edit to uncomment out the line

在快速编辑 windows/system32/drivers/etc/host 文件以取消注释该行后

127.0.0.0 localhost

127.0.0.0 本地主机

My page times went back to normal. Might be someone else is having this problem recently and seeing as this thread ranks top in Google then good luck!

我的页面时间恢复正常。可能是其他人最近遇到了这个问题,看到这个帖子在谷歌排名第一,祝你好运!

回答by Insensus

I'm not too familiar with apache but perhaps not specifying a port defaults to :80 and adding this would magically fix everything?

我对 apache 不太熟悉,但可能没有指定默认端口为 :80 并添加它会神奇地解决所有问题?

<VirtualHost 127.0.0.1:443>
    ServerName www.mysite.com
    ServerAlias mysite.com
    DocumentRoot "C:/xampp/htdocs/mysite"
</VirtualHost>

回答by Michal Seidler

I use my own domains (ended with .lc) for development web application on localhost. I will describe simple solution for dynamic .lc domains and development enviroment that works without dependency on internet connection.

我使用自己的域(以 .lc 结尾)在本地主机上开发 Web 应用程序。我将描述动态 .lc 域和开发环境的简单解决方案,该解决方案不依赖于互联网连接。

I wrote about it on my blog too: http://www.michalseidler.com/development/localhost-development-enviromet-for-php/

我也在我的博客上写过它:http: //www.michalseidler.com/development/localhost-development-enviromet-for-php/

For this example i try describe configuration of local dynamic domains *.lc with Wamp Server. I have my projects stored in C:\wamp\www\projects\projectname\ and i use dynamic maping projectname.lc. This means that i can access every project directory with domain [project direktory name].lc

对于此示例,我尝试使用 Wamp Server 描述本地动态域 *.lc 的配置。我的项目存储在 C:\wamp\www\projects\projectname\ 中,我使用动态映射 projectname.lc。这意味着我可以使用域 [project direktory name].lc 访问每个项目目录

Step 1 – configuration of local WAMP server

步骤 1 – 配置本地 WAMP 服务器

First of all you need place configuration of *.lc domain into httpd.conf:

首先,您需要将 *.lc 域的配置放入 httpd.conf:

<VirtualHost 127.0.0.1>
ServerName lc
ServerAlias *.lc
DocumentRoot "C:\wamp\www\projects"
</VirtualHost>;

You need insert .htaccess file into projects direktory (in my example into: C:\wamp\www\projects) this configuration maps *.ls domains to project direktories. Ex.: If you have sources in direktory ?myapp‘ you can use www.myapp.lc to open it in browser.

您需要将 .htaccess 文件插入项目目录(在我的示例中为:C:\wamp\www\projects),此配置将 *.ls 域映射到项目目录。例如:如果您在目录“myapp”中有源代码,您可以使用 www.myapp.lc 在浏览器中打开它。

RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^\.]*)\.([^\.]*)$
RewriteRule (.*) http://www.%1.%2/ [L,R=301]

RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.([^.]+)\.([^\.]*)$ [NC]
RewriteRule ^(.*)$ http://%1.%2.%3/ [L,R=301]

RewriteCond %{REQUEST_URI} !^projects/
RewriteCond %{REQUEST_URI} !^/projects/
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)\.([^\.]*)\.([^\.]*)$
RewriteRule (.*) %3/ [DPI] 

After this changes restart Wamp Server

在此更改后重新启动 Wamp 服务器

Step 2 – configuration of local DNS server

步骤 2 – 配置本地 DNS 服务器

Because we can't use *.lc in Windows host file we need instal local DNS server. I choose Acrylic DNS Server because it is very simple for configuration.

因为我们不能在 Windows 主机文件中使用 *.lc 我们需要安装本地 DNS 服务器。我选择 Acrylic DNS Server 因为它的配置非常简单。

After instalation find AcrylicHosts file (C:\Program Files (x86)\Acrylic DNS Proxy) and insert new line:

安装后找到 AcrylicHosts 文件 (C:\Program Files (x86)\Acrylic DNS Proxy) 并插入新行:

127.0.0.1 *.lc

This is only DNS configuration we need so restart Acrylic DNS service.

这只是我们需要的 DNS 配置,因此重新启动 Acrylic DNS 服务。

Step 3 – configuration of network adapter

步骤 3 – 配置网络适配器

Final step is install new fake network adapter and assign DNS server: 1.Click the Start menu. 2.Search for “cmd“. 3.Right-click on “cmd” and select “Run as Administrator” 4.Enter “hdwwiz.exe” 5.In the ?Welcome to the Add Hardware Wizard“, click Next. 6.Select ?Install the hardware that I manually select from a list (Advanced)“ and click Next. 7.Scroll down and select ?Network adapters“ and click Next. 8.Select under Manufacturer ?Microsoft“ and then under Network Adapter ?Microsoft Loopback Adapter“ and click Next.

最后一步是安装新的假网络适配器并分配 DNS 服务器: 1. 单击开始菜单。2.搜索“cmd”。3.右键单击“cmd”并选择“以管理员身份运行” 4.输入“hdwwiz.exe” 5.在“欢迎使用添加硬件向导”中,单击“下一步”。6.选择“安装我从列表中手动选择的硬件(高级)”,然后单击下一步。7.向下滚动并选择“网络适配器”,然后单击下一步。8.在Manufacturer ?Microsoft“下选择网络适配器?Microsoft Loopback Adapter”,然后单击下一步。

In next step you must change TCP/IP settings of new created adapter: 1.Log on to the computer by using the Administrator account. 2.Click Start, point to Control Panel, and click Network Connections. 3.Right-click the Loopback connection and then click Properties. 4.In the This connection uses the following items box, click Internet Protocol (TCP/IP), and then click Properties. The Internet Protocol (TCP/IP) Properties dialog box appears.

在下一步中,您必须更改新创建的适配器的 TCP/IP 设置: 1. 使用管理员帐户登录到计算机。2. 单击开始,指向控制面板,然后单击网络连接。3.右键单击环回连接,然后单击属性。4.在此连接使用以下项目框中,单击 Internet 协议 (TCP/IP),然后单击属性。出现 Internet 协议 (TCP/IP) 属性对话框。

IP addess: 192.168.1.1
Subnet mask: 255.255.255.0
Default Gateway: empty

Prefered DNS server: 127.0.0.1

Now close all dialogs and its done! You can try open [your project name].lc

现在关闭所有对话框并完成!您可以尝试打开[您的项目名称].lc