Apache中的httpd-vhosts.conf和.htacces动态虚拟主机自动化(XAMPP) [英] Automation of Dynamic Virtual Host in Apache httpd-vhosts.conf and .htacces (XAMPP)
问题描述
我试图建立在我的本地开发机器一定程度的自动化。通常情况下,我开始我的所有项目,像这样
I am trying to set up some automation on my local dev machine. Normally I start all my projects like this
打开Hosts文件,设置如下 127.0.0.1 example.com www.example.com
DNS条目
打开的httpd-vhosts.conf
文件并添加一个条目,像这样低于
Open Hosts file, set up a DNS entry like 127.0.0.1 example.com www.example.com
Open httpd-vhosts.conf
file and add an entry, something like this below
<VirtualHost *>
DocumentRoot "/path/to/xampp/htdocs/example.com"
ServerName example.com
ServerAlias www.example.com
</VirtualHost>
我试图做到的,是建立环境,这样的方式,我不应该在我的的httpd-vhosts.conf添加此项
每一次,我想工作在新主机上。
What I am trying to achieve is to set up the environment in such a way that, I shouldn't have add this entry in my httpd-vhosts.conf
every time I want to work on a new host.
而我想在的httpd-vhosts.conf
,可以处理所有的域,并把它们映射到各自的目录中一个全局条目
Rather I would like to have one global entry in httpd-vhosts.conf
that can handle all the domains and map them to their respective directories
所以,如果我有记录在我的主机列表
文件中像这样
so if I have a list of records in my hosts
file like this
127.0.0.1 example.com
127.0.0.1 mysite.com
127.0.0.1 google.com
127.0.0.1 abc.com
和这样的文件夹结构
/htdocs/
/example.com/
/mysite.com/
/google.com/
/abc.com/
它应该会自动映射到这些目录中不添加在的httpd-vhosts.conf的
文件。虚拟主机
记录的需要
It should automatically map to those directories without the need of adding the VirtualHost
record in the httpd-vhosts.conf
file.
我可以猜到,这可以通过添加的httpd-vhosts.conf通配符项来实现
(我不知道怎么做),然后加入一些重定向规则的.htaccess
文件放在 / htdocs中/
将它们映射到他们的目录。
I can guess that this can be achieved by adding a wildcard entry in httpd-vhosts.conf
(I don't know how) and then adding some redirect rules in .htaccess
file placed in /htdocs/
to map them to their directories.
所以最终,
- 如何设置在
的httpd-vhosts.conf通配符项
- 如何设置精确的重写
/ htdocs中/
在
的.htaccess
文件规则
- How to set up a wildcard entry in
httpd-vhosts.conf
- How to set up exact rewrite rules in
.htaccess
file in/htdocs/
这是东西,我想到了,也有可能是一个更好的方式来做到这一点。
This is something that I thought of, there might also be a better way to do this.
推荐答案
通过放置一个星号( *
)的域名,在那里你通常放在前面 WWW
1. How to set up a wildcard entry in httpd-vhosts.conf
By placing an asterix (*
) in front of the domain name, where you'd normally place www
<VirtualHost *>
DocumentRoot "/path/to/xampp/htdocs/example.com"
ServerName example.com
ServerAlias *.example.com
</VirtualHost>
2。如何设置精确的在/ htdocs中/
.htaccess文件重写规则
2. How to set up exact rewrite rules in .htaccess file in /htdocs/
RewriteEngine On
#RewriteCond %{HTTP_HOST} ^!example.com #(optional) do not redirect base domain
RewriteCond %{HTTP_HOST} (?(?=[a-z0-9\-\_]+\.[a-z0-9\-\_]{2,}$).+|[a-z0-9\-\_]+\.[a-z0-9\-\_]{2,})$
RewriteRule ^$ http://localhost/%0
所有这些领域:
All those domains:
www.example.com
img.example.com
cdn.example.com
static.img.example.com
将被重定向到 /example.com
。
说明
在什么上面呢,本质上,是需要在最后两个字符串由perdiod分离,将您重定向到新发现的文件夹。
What the above does, essentially, is it takes the last two strings separated by a perdiod and redirects you to the newly found folder.
从技术上看:
这正则表达式使用如果当时|其他
模式。如果 1
被发现,然后执行 2
做别的 3
This regex uses if then|else
pattern. If 1
is found, then do 2
else do 3
.
做一个测试,然后进行以下两个选项之一取决于文本(的结果?(?= [A-Z0-9 -_] + [A-Z0 -9 -_] {2} $)+ |,[A-Z0-9 -_] + [A-Z0-9 -_] {2} $)
-
(如果)
断言,下面的正则表达式可以匹配,开始在该位置(正向前查找)( ?= [A-Z0-9 \ - \ _] + \ [A-Z0-9 \ - \ _] {2} $)
- 在匹配单个字符present以下
列表[a-Z0-9 \ - \ _] +
- 之间的一个,不限次数,多次越好,回馈在需要(贪婪)
+
- 在A和Z
AZ
之间的范围内的字符 - 在之间的0范围内的字符和9
0-9
- 系统 - 字符
\ -
- 系统_字符
\ _
- 之间的一个,不限次数,多次越好,回馈在需要(贪婪)
- 在匹配单个字符present以下
(IF)
Assert that the regex below can be matched, starting at this position (positive lookahead)(?=[a-z0-9\-\_]+\.[a-z0-9\-\_]{2,}$)
- Match a single character present in the list below
[a-z0-9\-\_]+
- Between one and unlimited times, as many times as possible, giving back as needed (greedy)
+
- A character in the range between "a" and "z"
a-z
- A character in the range between "0" and "9"
0-9
- A - character
\-
- A _ character
\_
- Between one and unlimited times, as many times as possible, giving back as needed (greedy)
- Match a single character present in the list below
- 2至无限次,多次越好,回馈在需要(贪婪)
{2}
- 在A和Z
AZ
之间的范围内的字符 - 在之间的0范围内的字符和9
0-9
- 系统 - 字符
\ -
- 系统_字符
\ _
- Between 2 and unlimited times, as many times as possible, giving back as needed (greedy)
{2,}
- A character in the range between "a" and "z"
a-z
- A character in the range between "0" and "9"
0-9
- A - character
\-
- A _ character
\_
- 匹配任何单个字符不是一个换行符
+
- 之间的一个,不限次数,多次越好,回馈在需要(贪婪)
+
- Match any single character that is not a line break character
.+
- Between one and unlimited times, as many times as possible, giving back as needed (greedy)
+
- 在匹配单个字符present以下
列表[a-Z0-9 \ - \ _] +
- 之间的一个,不限次数,多次越好,回馈在需要(贪婪)
+
- 在A和Z
AZ
之间的范围内的字符 - 在之间的0范围内的字符和9
0-9
- 系统 - 字符
\ -
- 系统_字符
\ _
- 匹配字符。从字面上
\
- 在匹配单个字符present以下
列表[a-Z0-9 \ - \ _] {2}
- 2至无限次,多次越好,回馈在需要(贪婪)
{2}
- 在A和Z
AZ
之间的范围内的字符 - 在之间的0范围内的字符和9
0-9
- 系统 - 字符
\ -
- 系统_字符
\ _
- Match a single character present in the list below
[a-z0-9\-\_]+
- Between one and unlimited times, as many times as possible, giving back as needed (greedy)
+
- A character in the range between "a" and "z"
a-z
- A character in the range between "0" and "9"
0-9
- A - character
\-
- A _ character
\_
- Match the character "." literally
\.
- Match a single character present in the list below
[a-z0-9\-\_]{2,}
- Between 2 and unlimited times, as many times as possible, giving back as needed (greedy)
{2,}
- A character in the range between "a" and "z"
a-z
- A character in the range between "0" and "9"
0-9
- A - character
\-
- A _ character
\_
测试套件:
a.bc
ab.cd
abc.def
ab.cd.ef
12.34
12.34.56
1.2.3.4.5.67
cnn.com
no_country.for.old.men
这篇关于Apache中的httpd-vhosts.conf和.htacces动态虚拟主机自动化(XAMPP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!