2017-04-06 156 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