简体   繁体   中英

How to modify .htaccess

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM