Apache 配置域名跳转 (附Demo)

发布于:2025-04-12 ⋅ 阅读:(64) ⋅ 点赞:(0)

前言

🤟 找工作,来万码优才:👉 #小程序://万码优才/r6rqmzDaXpYkJZF

爬虫神器,无代码爬取,就来:bright.cn

以往写过详细的知识点:Apache httpd-vhosts.conf 配置详解(附Demo)

1. 基本知识

以下主要围绕在 WampServer 中配置 Apache 的域名跳转(也称为 虚拟主机跳转 或 域名映射跳转),主要是通过配置 Apache 的 httpd-vhosts.conf 文件和 Windows 的 hosts 文件来实现的

Apache 的虚拟主机功能允许为不同的域名配置不同的网站根目录、端口、跳转规则等
比如访问 http://mytest.local,跳转到本地的 D:/wamp64/www/mytest 网页

Apache 配置文件相关路径
在 WampServer 中:

  • 主配置文件:
    D:/wamp64/bin/apache/apache2.x.x/conf/httpd.conf

  • 虚拟主机配置文件:
    D:/wamp64/bin/apache/apache2.x.x/conf/extra/httpd-vhosts.conf
    默认未启用,需要确保主配置文件中有这行:
    Include conf/extra/httpd-vhosts.conf

在这里插入图片描述

Windows Hosts 文件
用于将某个域名映射到本地 IP
比如:127.0.0.1 mytest.local
路径:C:\Windows\System32\drivers\etc\hosts

2. Demo

在你的 Wamp 根目录,比如:

D:/wamp64/www/mytest/index.html
内容如下:

<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="refresh" content="0; url=https://www.baidu.com">
  <title>跳转中...</title>
</head>
<body>
  <p>页面正在跳转中...</p>
</body>
🔧 第二步:配置 Apache 虚拟主机 编辑文件:

D:/wamp64/bin/apache/apache2.x.x/conf/extra/httpd-vhosts.conf
添加以下内容:

<VirtualHost *:80>
    ServerAdmin webmaster@mytest.local
    DocumentRoot "D:/wamp64/www/mytest"
    ServerName mytest.local
    ErrorLog "logs/mytest.local-error.log"
    CustomLog "logs/mytest.local-access.log" common
</VirtualHost>

重启 WampServer 的 Apache 服务

打开浏览器访问:http://mytest.local,会看到跳转到https://www.baidu.com

3. 实战

配置域名之后 跳转这两个html文件,一个是测试库,一个是正式库!

在这里插入图片描述

简易代码如下,主要用于生成code 以及state的代码解析!

在这里插入图片描述

后续新增html文件,不需要重编译,本身是脚本文件!

在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到