大网数据广告活动
服务器运维问答

Create Sub Domain - Apache Virtual Host on LocalHost

· 52CE 我爱测

My Question in the title may be wrong, but I have no other Idea how to word it. (Experts please help with this?). Basically sub domains works on my Development Machine when I got to URL http://subdomain.localhost . What I am tryi…

问题原文

原帖链接 · Jacques Koekemoer · CC BY-SA 3.0

My Question in the title may be wrong, but I have no other Idea how to word it. (Experts please help with this?).

Basically sub domains works on my Development Machine when I got to URL http://subdomain.localhost. What I am trying to achieve is loading that same website using http://subdomain.jacques. jacques being the name of my computer.

This is what I have in my hosts file in windows.

127.0.0.1   localhost
localhost   localhost
jacques localhost

subdomain.127.0.0.1 subdomain.localhost
subdomain.localhost subdomain.localhost
subdomain.jacques   subdomain.localhost

This is what I have in the vhosts.conf file.

ServerName localhost
DocumentRoot "c:\web"

<VirtualHost jacques:80 jacques *:80>
    <Directory "c:\web">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Order Allow,Deny
        Allow from all
        Require all granted
    </Directory>
    ServerName localhost
    ServerAlias localhost
    DocumentRoot "c:\web"
</VirtualHost>

<VirtualHost subdomain.jacques *:80>
    ServerName subdomain.localhost
    ServerName subdomain.jacques
    ServerAlias subdomain.localhost
    ServerAlias subdomain.jacques
    DocumentRoot "C:\xampp\htdocs\subdomain"
    DirectoryIndex index.html index.php
    <Directory "C:\xampp\htdocs\subdomain">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Order Allow,Deny
        Allow from all
        Require all granted
    </Directory>
</VirtualHost>

What I have tried from reading the documentation on the Apache website:

  • Adding the PC name to the <VirtualHost> tag.
  • Adding another ServerName/ServerAlias to the Virtual Host
  • Adding entries into the hosts file in Windows

I have no idea what I am doing wrong, so even if you can answer the question, please point me in the right direction.

System Details

  • OS: Windows 10
  • Apache Version: 2.4
  • PHP Version: 5.4 (Not sure if this is useful or even relevant.)
  • Apache Install Path c:\amp\apache
  • PHP Install Path c:\amp\php

已采纳解答

原帖链接 · ALex_hha · CC BY-SA 3.0

This is what I have in my hosts file in windows.

this is wrong format. Must be - "ip address fqdn"

127.0.0.1 localhost jacques subdomain.localhost subdomain.jacques

As for virtual hosts

<VirtualHost *:80>
   ServerName localhost
   DocumentRoot "c:\web"

    <Directory "c:\web">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Order Allow,Deny
        Allow from all
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerName subdomain.localhost
    ServerAlias subdomain.jacques

    DocumentRoot "C:\xampp\htdocs\subdomain"
    DirectoryIndex index.html index.php

    <Directory "C:\xampp\htdocs\subdomain">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Order Allow,Deny
        Allow from all
        Require all granted
    </Directory>
</VirtualHost>

You can use domain (and any subdomain) lvh.me for testing purpose. As it resolves to 127.0.0.1. So there is no need to edit hosts file

# host lvh.me
lvh.me has address 127.0.0.1
lvh.me mail is handled by 10 mail.lvh.me.

# host subdomain.lvh.me
subdomain.lvh.me has address 127.0.0.1

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-03-18)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。