htaccess 选择任意 url
我想将尝试访问文件夹中文件的所有网址重定向到一个公共 php 页面,在该页面中记录正在下载的文件并检查用户是否登录,我正在尝试以下方式,但我没有获取所需结果
方法 1:
RewriteEngine on
RewrteRule ^(.+)$ index.php?file=$1
方法 2:
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 中做错了什么吗? 我可以使用方法 2,但文件名可以是任何内容 [可以包含所有字符],因此我需要一个涵盖所有 [如方法 1] 的正则
表达式
I want to redirect all urls trying to access files in a folder to a common php page where I log the file being download and also checks whether the user is logged in or not, I'm trying the following way, but I'm not getting required result
Method 1:
RewriteEngine on
RewrteRule ^(.+)$ index.php?file=$1
Method 2:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9\-]+)$ index.php?file=$1
My index.php
<php echo 'file - ' . $_REQUEST['file']; ?>
When I'm using the URL as http://localhost/next/files/Cool
Output Method 1 :
file - index.php
Output Method 2 :
file - Cool
Could you please tell me what is the wrong I'm doing in Method-1.
I could use Method-2 but the fileName could be anything [could contain all characters], so I need a regular expression which covers all [like in Method-1]
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
方法 1 的问题在于您创建了无休止的重定向。
由于所有文件都被重定向到index.php,index.php本身也被重定向到index.php,依此类推。
您必须从重定向中显式排除 index.php:
The problem with Method 1 is that you create an endless redirection.
Since all files are redirected to index.php, index.php itself is also redirected to index.php, and so on.
You have to explicitly exclude index.php from the redirection:
像这样编写你的第一条规则:
在你的index.php中将
file
查询参数读取为:Write your first rule like this:
And inside your index.php read
file
query parameter as: