RewriteRule有两个get参数

时间:2014-11-10 15:03:49

标签: regex .htaccess mod-rewrite parameters get

我有一个像这样的文件夹/文件结构:

www.example.com/a/<optional subfolder>/file.png

我希望有一个RewriteRule来传递&#34; a&#34;作为第一个参数和之后的所有内容(file.png或子文件夹/ file.png)到一个名为file.php的文件,所以如果用户访问

www.example.com/a/file.png (this is not an actual file path, it's somewhere else)

应该改写为

www.example.com/file.php?user=a&file=file.png

www.example.com/a/random/subfolders/here/file.png

将被重写为

www.example.com/file.php?user=a&file=random/subfolders/here/file.png

我尝试了很多我在互联网上找到的RewriteRules,其中一个甚至可以工作但不适用于子文件夹。由于我不了解RegEx的一件事,我非常感谢你的帮助^^

1 个答案:

答案 0 :(得分:1)

尝试将此规则添加到文档根目录中的htaccess文件中:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.+)$ /file.php?user=$1&file=$2 [L,QSA]