我想将所有尝试访问文件夹中的文件的urls重定向到一个通用的php页面,在那里我记录正在下载的文件,并检查用户是否已登录。我正在尝试以下方法,但我没有得到所需的结果
方法1:
RewriteEngine on
RewrteRule ^(.+)$ index.php?file=$1方法二:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9\-]+)$ index.php?file=$1我的index.php
<php echo 'file - ' . $_REQUEST['file']; ?>当我使用URL作为http://localhost/next/files/Cool输出方法1时:
file - index.php 输出方法2:
file - Cool 你能告诉我我在方法-1中做错了什么吗?我可以使用Method-2,但是fileName可以是任何内容,可以包含所有字符,所以我需要一个像Method-1一样涵盖所有内容的正则表达式
问候
发布于 2012-02-21 17:58:57
RewriteEngine on
RewriteRule ^(.*)$ index.php?file=$1 [NC,L]发布于 2012-02-21 18:20:30
方法1的问题是您创建了一个无休止的重定向。由于所有文件都被重定向到index.php,因此index.php本身也被重定向到index.php,依此类推。
您必须显式地从重定向中排除index.php:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.+)$ /index.php?file=$1发布于 2012-02-21 20:27:48
像这样写下你的第一条规则:
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中读取file查询参数如下:
echo 'file - ' . $_GET['file'];https://stackoverflow.com/questions/9375454
复制相似问题