htaccess 选择任意 url

发布于 2025-01-07 18:41:50 字数 652 浏览 1 评论 0原文

我想将尝试访问文件夹中文件的所有网址重定向到一个公共 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘虞初梦 2025-01-14 18:41:50
RewriteEngine on
RewriteRule ^(.*)$ index.php?file=$1 [NC,L]
RewriteEngine on
RewriteRule ^(.*)$ index.php?file=$1 [NC,L]
溺ぐ爱和你が 2025-01-14 18:41:50

方法 1 的问题在于您创建了无休止的重定向。
由于所有文件都被重定向到index.php,index.php本身也被重定向到index.php,依此类推。

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

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

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:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.+)$ /index.php?file=$1
梦行七里 2025-01-14 18:41:50

像这样编写你的第一条规则:

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'];

Write your first rule like this:

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]

And inside your index.php read file query parameter as:

echo 'file - ' . $_GET['file'];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文