我试图做一个dynamic的虚拟主机条目的目的是redirect到.mydomain.com所有stream量到/ var / www / html /这部分工作,但我也是一个重写条件,检查如果目录存在,如果不那么我想将所有stream量redirect到根/var/www/html/index.php。
这部分是不工作的,因为在我的testing期间,我注意到,没有重写variables工作,他们是空白的,我不明白为什么这里是我的虚拟主机configuration
# get the server name from the Host: header UseCanonicalName Off # this log format can be split per-virtual-host based on the first field # LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon CustomLog logs/access_log vcommon <VirtualHost *:80> ServerName mydomain.com ServerAlias www.mydomain.com VirtualDocumentRoot /var/www/html </VirtualHost> <VirtualHost *:80> ServerName vhosts.mydomain.com ServerAlias * VirtualDocumentRoot /var/www/html/%1 RewriteEngine on LogLevel warn rewrite:trace4 # If the request is not for a valid directory RewriteCond /var/www/html/%1 !-d RewriteCond %{REQUEST_FILENAME} !-d # If the request is not for a valid file RewriteCond %{REQUEST_FILENAME} !-f # If the request is not for a valid link RewriteCond %{REQUEST_FILENAME} !-l RewriteRule .* /var/www/html/index.php </VirtualHost>
你会注意到,我有几个使用variables,我可以使用testing时,看着我的日志文件
[Wed Jul 29 16:56:56.015818 2015] [rewrite:trace2] [pid 23898] mod_rewrite.c(475): [client xx.xx.xx.xx:4863] xx.xx.xx.xx - - [fake.mydomain.com/sid#7f5c89bcf570][rid#7f5c89ed5000/initial] init rewrite engine with requested uri / [Wed Jul 29 16:56:56.016050 2015] [rewrite:trace3] [pid 23898] mod_rewrite.c(475): [client xx.xx.xx.xx:4863] xx.xx.xx.xx - - [fake.mydomain.com/sid#7f5c89bcf570][rid#7f5c89ed5000/initial] applying pattern '.*' to uri '/' [Wed Jul 29 16:56:56.018361 2015] [rewrite:trace4] [pid 23898] mod_rewrite.c(475): [client xx.xx.xx.xx:4863] xx.xx.xx.xx - - [fake.mydomain.com/sid#7f5c89bcf570][rid#7f5c89ed5000/initial] RewriteCond: input='/var/www/html/' pattern='!-d' => not-matched [Wed Jul 29 16:56:56.018411 2015] [rewrite:trace1] [pid 23898] mod_rewrite.c(475): [client xx.xx.xx.xx:4863] xx.xx.xx.xx - - [fake.mydomain.com/sid#7f5c89bcf570][rid#7f5c89ed5000/initial] pass through /
即使%1通过虚拟目录就好了,但在mod重写规则中不起作用。 所有其他用途的variables产生相同的结果,任何帮助将不胜感激。
不确定它是否有所作为,但是它在Amazon Linux版本的AWS上运行。
%1不是整个Apacheconfiguration的一种全局variables。
在VirtualDocumentRoot中,它对应于“主机”名称的第一部分,在RewriteCond或RewriteRule它表示对前一个 RewriteCond的捕获组的反向引用。 请参阅Apache文档的RewriteCond
扔掉那部分:
RewriteCond /var/www/html/%1 !-d
您对%1的使用是错误的,您之前没有任何RewriteCond,所以%1是空string。 所以基本上你有一个条件,指出“如果/ var / www / html /不是一个目录,做…”
但显然/var/www/html是一个目录,这就是为什么你的日志行说
RewriteCond: input='/var/www/html/' pattern='!-d' => not-matched
UPADTE作为您的评论的后续行动:
要理解的是,Apache是由许多不同的模块组成的。 每个模块都有自己的语法。 这使整个非常强大,但可以混淆,如在你的问题。 每个模块都可以用一个名为%1的variables定义一个语法,代表每个模块中的其他东西。
这可能是我现在更好地理解你想要做什么:
/var/www/html/nosubdomain.php /var/www/html/nofile.php 第1步:获取子域
要获取子域,可以使用mod_rewrite的能力来定义和设置环境variables(这里的%1是RewriteCond中捕获的结果):
# Setting environment variable SUBDOMAIN = (subdomain).example.com RewriteCond %{HTTP_HOST} ^([^\.]*)\.example\.com$ RewriteRule . - [E=SUBDOMAIN:%1]
第二步:现在你可以在你的rewrite rules / cond中使用SUBDOMAINvariables,并且检查子域对应的目录是否存在。 如果不是,则redirect到定义的页面
# Test if subdomain directory exists, if not, redirect to a # standard page (/var/www/html/nosubdomain.php). RewriteCond /var/www/html/%{ENV:SUBDOMAIN} !-d RewriteRule .* /var/www/html/nosubdomain.php [L]
步骤3:检查是否存在请求的文件,如果不存在,发送标准页面( /var/www/html/nofile.php )。 为此,最好依靠Apache检查自己并使用ErrorDocument。 我们需要一个别名,因为你想让nofile.php文件在VirtualDocumentRoot之外( /var/www/html/subdomain/ )
Alias /nofile.php /var/www/html/nofile.php ErrorDocument 404 /nofile.php
把所有这些步骤放在你的指令RewriteEngine On应该带上一个工作的设置,当然你将不得不适应你的确切的用例。 我已经testing过这个设置,它可以像我想的那样工作,但是在Apache 2.2上。
如果您不在其他地方使用SUBDOMAIN,也可以将步骤1和步骤2组合在一起:
RewriteCond %{HTTP_HOST} ^([^\.]*)\.example\.com$ RewriteCond /var/www/html/%1 !-d RewriteRule .* /var/www/html/nosubdomain.php [L]