简体   繁体   English

如何修改.htaccess

[英]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. 当我链接到目录中的文件时,我想在php程序中将该文件作为参数打开。

For example, I have a directory temp , and files aa.txt , bb.txt , and test.php . 例如,我有一个目录temp ,文件aa.txtbb.txttest.php When I link to aa.txt it should be handled like test.php?f=aa.txt . 当我链接到aa.txt ,应该像test.php?f=aa.txt一样进行处理。

What do I change in the .htaccess file? .htaccess文件中有哪些更改?

the code in the test.php 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: 您想使用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]
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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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