2013-11-22 106 views
0

在Zend Framework(v1.12)應用程序中,我想強制調用特定的控制器(api)。通過URL重寫強制特定的Zend Framework控制器

我已經設法使任何請求是重寫任何請求(如http://example.com/foo/bar」或「http://example.com‘),它改寫爲http://example.com/api/ ...(如:http://example.com/api/foo/bar」或「http://example.com/api/ 「)具有以下的Apache重寫規則:

<VirtualHost *:80> 
    [...] 
    RewriteEngine On 
    RewriteCond %{REQUEST_URI} !^/?api/?.* [NC] 
    RewriteCond %{REQUEST_URI} !^/index\.php$ [NC] 
    RewriteRule ^/?(.*)$ /api/$1 [R,L] 
    [...] 
</VirtualHost> 

RewriteLogs說:

/initial] (2) init rewrite engine with requested uri /api/ 
/initial] (3) applying pattern '^/?(.*)$' to uri '/api/' 
/initial] (1) pass through /api/ 
/initial] (3) [perdir /var/www/example.com/public/] add path info postfix: /var/www/example.com/public/api -> /var/www/example.com/public/api/ 
/initial] (3) [perdir /var/www/example.com/public/] strip per-dir prefix: /var/www/example.com/public/api/ -> api/ 
/initial] (3) [perdir /var/www/example.com/public/] applying pattern '^.*$' to uri 'api/' 
/initial] (3) [perdir /var/www/example.com/public/] add path info postfix: /var/www/example.com/public/api -> /var/www/example.com/public/api/ 
/initial] (3) [perdir /var/www/example.com/public/] strip per-dir prefix: /var/www/example.com/public/api/ -> api/ 
/initial] (3) [perdir /var/www/example.com/public/] applying pattern '^.*$' to uri 'api/' 
/initial] (2) [perdir /var/www/example.com/public/] rewrite 'api/' -> 'index.php' 
/initial] (3) [perdir /var/www/example.com/public/] add per-dir prefix: index.php -> /var/www/example.com/public/index.php 
/initial] (2) [perdir /var/www/example.com/public/] strip document_root prefix: /var/www/example.com/public/index.php -> /index.php 
/initial] (1) [perdir /var/www/example.com/public/] internal redirect with /index.php [INTERNAL REDIRECT] 
/initial/redir#1] (2) init rewrite engine with requested uri /index.php 
/initial/redir#1] (3) applying pattern '^/?(.*)$' to uri '/index.php' 
/initial/redir#1] (1) pass through /index.php 

但這仍然允許用戶通過http://example.com/index.php訪問應用程序的主頁,我無法設法禁止此操作。

我試圖阻止:

<VirtualHost *:80> 
    [...] 
    # Rewrite code from above 
    [...] 
    <Location /index.php> 
     Order Allow,Deny 
     Deny from all 
    </Location> 
    [...] 
</VirtualHost> 

但作爲一個ZF的應用,每個請求具有傳遞到的index.php(引導程序):標準ZF重寫規則重寫一切的index.php:

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} -s [OR] 
RewriteCond %{REQUEST_FILENAME} -l [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^.*$ - [NC,L] 
RewriteRule ^.*$ index.php [NC,L] 

因此,我得到一個「禁止 - 您無權訪問此服務器上的/index.php」。爲每個請求。

我可以在用於重寫請求時允許訪問「index.php」並禁止直接訪問它嗎?

我確信這可以通過ZF應用程序的路由器來完成,但是想要避免更改應用程序的PHP源代碼。

回答

0

使用ENV:REDIRECT_STATUS變量(見mromainecontribution"Hidden features of mod_rewrite" community wiki我已經成功地禁止直接訪問index.php

RewriteEngine On 

# Force controller "api" 
RewriteCond %{REQUEST_URI} !^/?api/?.* [NC] 
RewriteCond %{REQUEST_URI} !^/index\.php$ [NC] 
RewriteRule ^/?(.*)$ /api/$1 [R,L] 

# Deny direct access to "index.php" 
RewriteCond %{ENV:REDIRECT_STATUS} ^$ 
RewriteRule index\.php$ - [NC,F] 

測試用例: