2010-08-09 91 views
0

當我鏈接到目錄中的文件時,我想在php程序中將該文件作爲參數打開。如何修改.htaccess

例如,我有一個目錄temp和文件aa.txtbb.txttest.php。當我鏈接到aa.txt它應該像test.php?f=aa.txt一樣處理。

我在.htaccess文件中更改了什麼?

在test.php的代碼

<?php 
$f=$_GET['f']; 
if(@file_exists($f)){ 
    $inhoud = file_get_contents($f); 
}else{ 
    $inhoud = "Not found\n"; 
} 
print "hallo <hr>".$inhoud; 

?> 

回答

2

你想用mod_rewrite,並這樣定義目錄中的.htaccess文件中的以下規則,你希望它適用於:

# Enable mod_rewrite 
RewriteEngine on 

# If the request is an actual file and not test.php... 
RewriteCond %{REQUEST_FILENAME} !test.php$ 
RewriteCond %{REQUEST_FILENAME} -f 

# ... then rewrite the URL to pass the filename as a parameter to test.php 
RewriteRule /(.*)$ test.php?f=$1 [QSA] 
+0

我測試過,它顯示了文件的原始內容(a.txt)我的腳本只是把結果放在你的面前 – Grumpy 2010-08-09 21:27:44

+0

聽起來像你的test.php文件沒有做你想做的事t做... – 2010-08-09 21:42:39

+0

我添加了test.php的源碼 – Grumpy 2010-08-09 21:58:54

0
RewriteEngine On 
RewriteCond %{REQUEST_URI} !test.php #Do not rewrite the test.php file 
RewriteCond %{REQUEST_URI} -f  #Requested filename exists 
RewriteRule (.*) test.php?f=$1 [QSA,L] 
+0

嗨,它給出一個錯誤 – Grumpy 2010-08-09 21:28:03