Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php htaccess选择任意url_Php_Regex_.htaccess_Mod Rewrite - Fatal编程技术网

Php htaccess选择任意url

Php htaccess选择任意url,php,regex,.htaccess,mod-rewrite,Php,Regex,.htaccess,Mod Rewrite,我想将试图访问文件夹中文件的所有URL重定向到一个公共php页面,在该页面中,我记录正在下载的文件,并检查用户是否登录,我尝试以下方法,但没有得到所需的结果 方法1: RewriteEngine on RewrteRule ^(.+)$ index.php?file=$1 方法2: RewriteEngine on RewriteRule ^([a-zA-Z0-9\-]+)$ index.php?file=$1 My index.php <php echo 'file - ' . $_

我想将试图访问文件夹中文件的所有URL重定向到一个公共php页面,在该页面中,我记录正在下载的文件,并检查用户是否登录,我尝试以下方法,但没有得到所需的结果

方法1:

RewriteEngine on
RewrteRule ^(.+)$ index.php?file=$1
方法2:

RewriteEngine on
RewriteRule ^([a-zA-Z0-9\-]+)$ index.php?file=$1
My index.php

<php echo 'file - ' . $_REQUEST['file']; ?>
输出方法2:

file - Cool 
你能告诉我在方法1中我做错了什么吗。 我可以使用方法2,但文件名可以是任何内容[可以包含所有字符],因此我需要一个涵盖所有内容的正则表达式[就像方法1中那样]


关于

方法1的问题是您创建了一个无止境的重定向。
RewriteEngine on
RewriteRule ^(.*)$ index.php?file=$1 [NC,L]
由于所有文件都被重定向到index.php,index.php本身也被重定向到index.php,依此类推

必须从重定向中显式排除index.php:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.+)$ /index.php?file=$1

写下你的第一条规则如下:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-f
# forward requests to index.php as a query parameter
RewriteRule ^(.*)$ index.php?file=$1 [QSA,L]
在index.php中读取
文件
查询参数,如下所示:

echo 'file - ' . $_GET['file'];

对不起,力不从心。但是这是有效的:[L,QSA]从你能让我知道这些是什么吗?在这里你可以找到所有的标志说明这显然不起作用,因为重写应该特别重写现有文件到index.php。谢谢,小问题什么是文件名包含空间。说picture(1).jpg,那么它只匹配picture(留下(1).jpg)-这里有什么建议吗@apfelbox请开始阅读mod_rewrite,
RewriteCond%{REQUEST_FILENAME}-f
将跳过任何物理文件的重写规则。我的回答中也有评论,但您可能没有注意到。@user237865:如果您单击链接或在浏览器中键入
http://localhost/picture (1) .jpg
它将自动成为
http://localhost/picture%20(1) .jpg
。在这种情况下,如果您在代码中执行
$\u GET['file']
,您将获得
图片(1)。jpg
@anubhava我已经知道mod_rewrite。问题负责人的要求是——据我所知——他想将URL重写为现有文件,用PHP进行一些预处理。这在您的解决方案中是不可能的,请参阅我上面的评论。
echo 'file - ' . $_GET['file'];