Apache的mod_rewrite任何子域(县)根域,除非存在,因为virtualdocumentroot [英] Apache mod_rewrite ANY subdomain(s) to root domain, unless existent as virtualdocumentroot
问题描述
假设你有一个Apache2的虚拟主机设置,是这样的:
Say you've got an Apache2 virtual host setup, something like this:
/htdocs/parent1.com
/htdocs/sub1.parent1.com
/htdocs/sub2.parent1.com
/htdocs/parent2.net
/htdocs/parentn.org
假设你想与 VirtualDocumentRoot / htdocs中/%0
来做到这一点,这样就可以不与Apache配置修修补补添加和删除虚拟主机。这一点很重要:请请htaccess的文件,每一个虚拟主机自带或进入时间不搞乱或httpd.conf中 - 该主机是否是一个父域或没有。事实上,说你正在使用的AllowOverride无
。
Say you'd like to do this with VirtualDocumentRoot /htdocs/%0
, so that you can add and remove virtual hosts without tinkering with your Apache configuration. That's important: please please no messing with htaccess files or httpd.conf every time a virtual host comes or goes - whether that host is a parent domain or not. In fact, say you're using AllowOverride None
.
无论如何,问题是,如何可能你的301重定向不存在的子域到相应的父域不存在重定向子域?
Anyway, the question is, how might you 301 redirect non-existent sub-domains to their corresponding parent domains without redirecting existent sub-domains?
推荐答案
我可能已经解决了我自己的问题。不过,我想AP preciate任何反馈意见,如果有人发现了一个问题,我在做什么。
I may have solved my own problem. However I would appreciate any feedback if somebody finds a problem with what I'm doing.
单独下列叶子到任意子域的任何请求,只要存在一个相应的文档根;但重定向到不在文件系统中存在的子域的任何请求。
The following leaves alone any request to an arbitrary subdomain, as long as there exists a corresponding document root; but redirects any request to a subdomain which does not exist in the filesystem.
<IfModule rewrite_module>
RewriteEngine On
RewriteMap lowercase int:tolower
RewriteCond "/htdocs/${lowercase:%{HTTP_HOST}}" !-d
RewriteCond %{HTTP_HOST} "\.([^\.]+\.[^\.]+)$"
RewriteRule ^/(.*)$ "http://%1/$1" [R=301]
</IfModule>
让我来设置通配符DNS和使用基于域名的虚拟主机,不碰任何配置设置。此外,还有不涉及htaccess的。只要您的文件夹中包含/htdocs/[host.]domain.tld的名字,你是启动和运行。据我所知,这并没有真正使用SSL / TLS工作(presumably用的东西做%{HTTP_HOST}
?),但安全站点同等很少,通过IP地址比主机名比较好解决。
Allows me to setup wildcard DNS and use name-based virtual hosting, without touching any configuration settings. Also, there's no htaccess involved. Just make your folder with any name like "/htdocs/[host.]domain.tld" and you're up and running. As far as I can tell, this doesn't really work with SSL/TLS (presumably something to do with %{HTTP_HOST}
?), but secure sites are comparably few and better resolved by IP address than by hostname.
这篇关于Apache的mod_rewrite任何子域(县)根域,除非存在,因为virtualdocumentroot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!