2017-04-06 161 views
0

到子目录的Apache 2.4,我需要创建下面的规则,并把它在我的虚拟主机文件:阿帕奇重写例外

  1. 即成从某个DNS从文件的所有请求在特定目录 (例子,请求“http://www.mydns.com/login.php”应提供来自subdir“www/subdir/login.php”的文件请求“http://www.mydns.com/a/b/c/some.php”应该从subdir“www/subdir/a/b/c/some.php”提供文件)
  2. Ignore这个规则对于一些目录(请求“http://www.mydns.com/cache/myjs.js”应该从“www/cache /”正常服务)
  3. 如果请求包含的子目录,无视规则(请求“http://www.mydns.com/subdir/login.php”正常供应)
  4. 将规则应用到任何端口/协议(HTTP,HTTPS,其他港口像8080)
  5. 网址不应更改/重定向用户浏览器

我一直在尝试使用虚拟主机文件内的重写规则(每个DNS的一个文件),但一直很难与它。

任何帮助是极大的赞赏:)

更新

@spacepickle,感谢您的答复。仍然没有去,我可能没有给出所有需要的细节,所以它在这里。 我会在下面发布我的conf文件和测试。

的conf文件:

<VirtualHost *:80> 
    ServerName my.domain.com 
    ServerAlias my2.domain.com 
    DocumentRoot /etc/www 

    RewriteEngine On 
    RewriteRule ^/subdir/ - [PT,L] 
    RewriteRule ^/cache/ - [PT,L] 
    RewriteRule ^/(.*)$ /subdir/$1 [PT,L] 
</VirtualHost> 

GET http://my.domain.com。 预计重写http://my.domain.com/subdir。 得到了fcgi错误:在此服务器上找不到请求的URL /subdir/php5.fcgi/subdir/index.php。

GET http://my.domain.com/subdir。 预计什么都没发生(忽略重写)。 得到重写:在此服务器上未找到请求的URL/subdir/subdir。

GET http://my.domain.com/cache。 预计什么都没发生(忽略重写)。 得到重写:在此服务器上未找到请求的URL/subdir/cache。

一个测试案例工作: GET http://my.domain.com/clients/tests/index.html。 预期,结果被改写为:http://my.domain.com/subdir/clients/tests/index.html

+0

更新的答案与'^重写规则/子目录(/.*)? - [PT,L]'和'RewriteRule ^/cache(/.*)? - [PT,L]' - 再试一次? – spacepickle

回答

0

UPDATE有时它最好还是远离mod_rewrite的路程。我的原始答案在下面使用了mod_rewrite,但使用mod_alias继承了一个更清晰的解决方案,我认为这解决了OP的特定用例。 mod_rewrite解决方案将变得更加复杂,而使用mod_alias应该保持简单(希望)。

创建一个基地别名。INC文件

Alias "/subdir" "${DocumentRoot}/subdir" 
Alias "/cache" "${DocumentRoot}/cache" 
Alias "/" "${DocumentRoot}/subdir/" 

然后包括这就像每个虚拟主机定义:

<VirtualHost *:80> 
    ServerName www.example.com 
    Define DocumentRoot /var/www/www 
    DocumentRoot ${DocumentRoot} 

    Include base-alias.inc 

</VirtualHost> 

这里要注意的重要行是Define DocumentRoot /var/www/www:这可以让你的基本目录的方式传递到包含的文件。

这应该可以很好地用于php,并且当重定向由apache模块发布时,由于与下一个mod_rewrite策略不同,您访问的url是服务器认为是资源规范URL的内容。

原始回答使用mod_rewrite

以下四行添加到文件中(基rewrite.inc):

RewriteEngine On 
RewriteRule ^/subdir(/.*)? - [PT,L] 
RewriteRule ^/cache(/.*)? - [PT,L] 
RewriteRule ^/(.*)$ /subdir/$1 [PT,L] 

EDIT加入(/.*)?到第一两个URL的结尾这意味着,例如,只有子目录作为文件夹(有或没有斜杠)或低于第一个规则的一部分。这可以防止类似/ subdirbutnot /你好相匹配

然后包括这从virtualhosts

<VirtualHost *:80> 
    ServerName www.example.com 
    DocumentRoot /var/www/www/ 

    Include base-rewrite.inc 
</VirtualHost> 

或者添加ServerAlias线使用多个名称:

<VirtualHost *:80> 
    ServerName www.example.com 
    ServerAlias new.example.com 
    DocumentRoot /var/www/www/ 

    Include base-rewrite.inc 
</VirtualHost> 

添加更多virtualhosts不同的文档根目录,如果需要的话

<VirtualHost *:80> 
    ServerName other.example.com 
    DocumentRoot /var/www/other/ 

    Include base-rewrite.inc 
</VirtualHost> 

或m矿石端口/协议

<VirtualHost *:443> 
    ServerName www.example.com 
    DocumentRoot /var/www/www/ 

    SSLEngine on 
    SSLCertificateFile  /etc/ssl/certs/ssl-cert-snakeoil.pem 
    SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key 

    Include base-rewrite.inc 
</VirtualHost> 

一种替代但可疑溶液

注意,该文件夹路径可以被动态地由被请求,一个虚拟主机内的主机名决定,但这种评估在路径环境变量这是一条安全漏洞的明确途径。我只是在展示本作的灵活性上提供一个演示:

基重写,02.inc

RewriteEngine On 

RewriteCond "%{ENV:ENV_HOST}" "" 
RewriteCond "%{HTTP_HOST}" "!^(www|other)\.example\.com$" 
RewriteRule ^/(.*) - [E=ENV_HOST:www] 

RewriteCond "%{ENV:ENV_HOST}" "" 
RewriteCond "%{HTTP_HOST}" "^(www|other)\.example\.com$" 
RewriteRule ^/(.*) - [E=ENV_HOST:%1] 

RewriteRule ^/subdir/(.*) /%{ENV:ENV_HOST}/subdir/$1 [PT,L] 
RewriteRule ^/cache/(.*) /%{ENV:ENV_HOST}/cache/$1 [PT,L] 
RewriteRule ^/(.*) /%{ENV:ENV_HOST}/subdir/$1 [PT,L] 

而虚拟主机定义:

<VirtualHost *:80> 
    ServerName www.example.com 
    ServerAlias other.example.com 
    DocumentRoot /var/www/ 

    Include base-rewrite-02.inc 
</VirtualHost> 
+0

谢谢,我用我的测试解决方案更新了原始帖子 – RudiBR

+0

@RudiBR我应该记得在比赛规则的末尾加入'(/.*)?' – spacepickle

+0

再次尝试这种修改,我明白这是无论如何需要,但测试结果是相同的。 – RudiBR