Apache 将子域路由到服务器中的目录

Apache 将子域路由到服务器中的目录,apache,Apache,如何在localhost中创建子域。在使用windows机器时,我试图编辑windows主机文件 127.0.0.1 localhost 127.0.0.1/subdomain subdomain.localhost 得到这样的东西 http://subdomain.localhost 更新: 首先,这个问题是在没有任何背景的情况下提出的,这次更新是为了提高质量。 通过编辑vhosts文件可以实现以下目的 虚拟主机: 现在我找到了一个更好的例子,使

如何在localhost中创建子域。在使用windows机器时,我试图编辑windows
主机
文件

  127.0.0.1               localhost
  127.0.0.1/subdomain     subdomain.localhost
得到这样的东西

http://subdomain.localhost

更新: 首先,这个问题是在没有任何背景的情况下提出的,这次更新是为了提高质量。

通过编辑vhosts文件可以实现以下目的

虚拟主机:

现在我找到了一个更好的例子,使用
重写规则
(适用于任何子域,而不仅仅是本地主机)

在.htaccess文件中:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.example.com
RewriteRule ^(.*)$ http://example.com/subdomains/sub/$1 [L,NC,QSA]
对于更一般的规则,我们可以替换最后两行:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/subdomains/%1/$1 [L,NC,QSA]
我不是mod_重写专家,请帮助改进和指导

谢谢。

我想这个问题以前已经解决了。可能会回答您的问题:

没问题。。。如果答案对您有效,请务必将其检查为已接受谢谢问题是我在localhost中有文件,我想添加一个子域,这是一个愚蠢的问题,但是你是否尝试为你的子域添加一个子目录?你的链接指向了一个与我回答的类似的示例,它只是一个不同的帮助站点!
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.example.com
RewriteRule ^(.*)$ http://example.com/subdomains/sub/$1 [L,NC,QSA]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/subdomains/%1/$1 [L,NC,QSA]