When I link to a file in a directory, I want to open that file as a param in a php program.
For example, I have a directory temp
, and files aa.txt
, bb.txt
, and test.php
. When I link to aa.txt
it should be handled like test.php?f=aa.txt
.
What do I change in the .htaccess file?
开发者_运维百科the code in the test.php
<?php
$f=$_GET['f'];
if(@file_exists($f)){
$inhoud = file_get_contents($f);
}else{
$inhoud = "Not found\n";
}
print "hallo <hr>".$inhoud;
?>
You want to use mod_rewrite, and define rules like the following within a .htaccess file in the directory you want it to apply to:
# 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]
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]
精彩评论