我有一个VirtualHost,我想有几个子域名。 (为了清楚起见,假设我的域名是example.com ,我只是想通过让foo.example.com工作,并从那里开始构build。)
我发现一个子域与我的框架非侵入性地工作最简单的方法是通过mod_rewrite代理到一个子path。 因此,path将在客户端的URL栏中显示为http://foo.example.com/(无论什么),而他们实际上将被提供http://foo.example.com/foo/(whatever) 。
我设法在我的VirtualHostconfiguration文件中这样做:
ServerAlias *.example.com RewriteEngine on RewriteCond %{HTTP_HOST} ^foo\.example\.com [NC] # <--- RewriteCond %{REQUEST_URI} !^/foo/.*$ [NC] # AND is implicit with above RewriteRule ^/(.*)$ /foo/$1 [PT]
(注意:很难find这个特定的工作组合,特别是在[RewriteRule]中,[PT]似乎是必须的,我无法用我在别处看到的例子[L] 。它不会显示任何东西或进入循环。也有一些浏览器似乎caching响应页面的糟糕的循环,一旦他们得到一个页面重新加载后修复它不会显示它正在工作!任何情况下 – 如果这部分可以做得更好。)
现在我想让什么http://foo.example.com/foo/(无论)提供取决于谁问。 如果请求来自外部,我希望客户端被Apache永久redirect,以便他们在浏览器中获得URL http://foo.example.com/(无论什么) 。 如果它来自mod_rewrite的内部,我想要的是由Web框架处理…这是不知道子域。
是这样的可能吗?
似乎你几乎在那里,不是吗?
使用基于REMOTE_ADDR的RewriteCond,例如:
# # Provide HTTP redirect "[R]" for network-external requests # For permanent redirects, use [R=301], but note cache concerns: # http://getluky.net/2010/12/14/301-redirects-cannot-be-undon/ # RewriteCond %{REMOTE_ADDR} !^10\.2\. RewriteCond %{HTTP_HOST} ^example\.com [NC] RewriteCond %{REQUEST_URI} ^/foo/.*$ [NC] RewriteRule ^/foo/(.*)$ http://foo.example.com/$1 [R] # # Pass-Through "[PT]" to subpath URL for subdomain requests # (Assumes that foo.example.com/foo would serve the same # content as example.com/foo, if not for the above rule) # RewriteCond %{HTTP_HOST} ^foo\.example\.com [NC] RewriteRule ^/(.*)$ /foo/$1 [PT]
在REMOTE_ADDR中针对10.2.xx地址使用匹配的示例来自http://httpd.apache.org/docs/2.2/rewrite/intro.html
我怀疑你必须使用“PT”,因为你的configuration中有一个别名。 或者是一个非直接的VirtualHost DocumentRoot。
无论如何,这似乎是在我的testing中伎俩:
<VirtualHost _default_:80> DocumentRoot /var/www/html RewriteEngine on RewriteCond %{HTTP_HOST} ^foo\.localhost [NC] RewriteCond %{REQUEST_URI} ^/foo/.*$ [NC] RewriteRule ^/foo/(.*)$ /$1 [R] RewriteCond %{HTTP_HOST} ^foo\.localhost [NC] # <--- RewriteCond %{REQUEST_URI} !^/foo/.*$ [NC] # AND is implicit with above RewriteRule ^/(.*)$ /foo/index.php?q=$1 [PT] </VirtualHost>
有了以下输出:
$ curl -D – foo.localhost / index.php
HTTP/1.1 200 OK Date: Wed, 06 Nov 2013 13:46:45 GMT Server: Apache/2.2.17 (Fedora) Vary: Host X-Powered-By: PHP/5.3.6 Content-Length: 55 Connection: close Content-Type: text/html; charset=UTF-8 Host: foo.localhost URI: /index.php Query: q=index.php
$ curl -D – foo.localhost / foo / index.php
HTTP/1.1 302 Found Date: Wed, 06 Nov 2013 13:46:51 GMT Server: Apache/2.2.17 (Fedora) Location: http://foo.localhost/index.php Content-Length: 293 Connection: close Content-Type: text/html; charset=iso-8859-1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>302 Found</title> </head><body> <h1>Found</h1> <p>The document has moved <a href="http://foo.localhost/index.php">here</a>.</p> <hr> <address>Apache/2.2.17 (Fedora) Server at foo.localhost Port 80</address> </body></html>
$ curl -D – foo.localhost /任何东西
HTTP/1.1 200 OK Date: Wed, 06 Nov 2013 13:46:59 GMT Server: Apache/2.2.17 (Fedora) Vary: Host X-Powered-By: PHP/5.3.6 Content-Length: 53 Connection: close Content-Type: text/html; charset=UTF-8 Host: foo.localhost URI: /anything Query: q=anything
$ curl -D – foo.localhost / foo /任何东西
HTTP/1.1 302 Found Date: Wed, 06 Nov 2013 13:47:04 GMT Server: Apache/2.2.17 (Fedora) Location: http://foo.localhost/anything Content-Length: 292 Connection: close Content-Type: text/html; charset=iso-8859-1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>302 Found</title> </head><body> <h1>Found</h1> <p>The document has moved <a href="http://foo.localhost/anything">here</a>.</p> <hr> <address>Apache/2.2.17 (Fedora) Server at foo.localhost Port 80</address> </body></html>