2012-12-08 46 views
0

我改寫的PHP文件的文件名與下面的htaccess代碼:重寫PHP文件名CGI與htaccess以及拒絕訪問PHP文件

ErrorDocument 404 /notfound.html 

AddType application/x-httpd-php .cgi 
RewriteEngine on 
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR] 
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR] 
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR] 
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR] 
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2}) 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php [F,L] 
RewriteRule ^(.*)\.cgi $1\.php 

我想要做什麼:我可以限制訪問php文件呢?例如,如果訪問者想看到page.php獲取404錯誤,並且當想要查看main.cgi頁面可以看到頁面沒有錯誤,是否有可能?
如果我不能只用htaccess來做到這一點,最好的解決方案是什麼?

+0

做你的QUERY_STRING檢查最終會做什麼?看起來_only_是否其中一個觸發_and_Request_Filename不是文件或目錄,RewriteRule會啓動嗎?您是否缺少Query_String檢查的RewriteRule? –

回答

1

你可以實現你想要的具有公共文件夾以外的PHP文件,不是有你的index.php文件包括他們

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php [F,L] 

和index.php文件類似

$validFiles = array('page', 'about', 'gallery'); 

$requestedFile = str_replace('.cgi', '', trim($_SERVER['REQUEST_URI'], '/'); 

if(in_array($requestedFile, $validFiles)) 
{ 
    // ../ goes into the parent folder, and code is a sibling of your public folder 
    include '../code/'.$requestedFile.'.php'; 
} 
else 
{ 
    // file not found stuff or index here 
} 

雖然我會質疑你爲什麼需要使用.cgi擴展名,這是否與你的主機有關,或者是遺留的需求?因爲htaccess你可以有乾淨的網址,像

mydomain.com/products/widget/

+0

感謝CodeMonkey,我想要有乾淨的URL並更改php原始的php文件。 – Root125

相關問題