mod_rewrite - 重定向到几个不同的 PHP 文件
我希望对 /files/ 的任何请求都重定向到一个 php 脚本,并将所有其他请求(不适用于特定文件类型)定向到另一个脚本。无论我如何尝试,我都无法让它发挥作用。这是来自我的 apache2 虚拟服务器配置文件:
<Directory /var/www/test/public>
RewriteEngine On
RewriteBase /files
RewriteRule ~files/(.+) file-handler.php?id=$1 [L,QSA]
RewriteRule !\.(pdf|gif|jpg|png|css|swf|txt|php|ico|html|js|xml|flv)$ /index.php?page=%{REQUEST_URI}
</Directory>
我也尝试过:
<Directory /var/www/test/public>
RewriteEngine On
RewriteBase /
RewriteRule !\.(pdf|gif|jpg|png|css|swf|txt|php|ico|html|js|xml|flv)$ /index.php?page=%{REQUEST_URI}
RewriteBase /files
RewriteRule ~files/(.+) file-handler.php?id=$1 [L,QSA]
</Directory>
I want any request to /files/ to be redirected to one php script, and all others (that aren't for specific file types) to be directed to another. No matter what I try I can't get this to work. This is from my apache2 virtual server config file:
<Directory /var/www/test/public>
RewriteEngine On
RewriteBase /files
RewriteRule ~files/(.+) file-handler.php?id=$1 [L,QSA]
RewriteRule !\.(pdf|gif|jpg|png|css|swf|txt|php|ico|html|js|xml|flv)$ /index.php?page=%{REQUEST_URI}
</Directory>
I also tried this:
<Directory /var/www/test/public>
RewriteEngine On
RewriteBase /
RewriteRule !\.(pdf|gif|jpg|png|css|swf|txt|php|ico|html|js|xml|flv)$ /index.php?page=%{REQUEST_URI}
RewriteBase /files
RewriteRule ~files/(.+) file-handler.php?id=$1 [L,QSA]
</Directory>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您使用过虚拟主机吗? <虚拟主机>部分应该有“RewriteOptions Inherit”
Have you used Virtual Hosts? < VirtualHost> section should have "RewriteOptions Inherit"
好吧,我没有解决这个问题,但找到了一个解决方法 - 我只是重定向到一个 PHP 文件,然后在其中执行逻辑。不知道为什么我之前没有想到这一点...
Well, I didn't solve this, but found a work around - I just redirect to a single PHP file and then do the logic in there. Don't know why I didn't think of that before...
我认为你的问题与 RewriteBase 和/或 ~file 匹配有关。您是否尝试过只使用一个基础,并将其用于您的规则?不完全确定你正在尝试使用你的规则,但是
重写引擎开启
重写库 /
RewriteRule ^files/(.+) /file-handler.php?id=$1 [L,QSA]
RewriteRule !.(pdf|gif|jpg|png|css|swf|txt|php|ico|html|js|xml|flv)$ /index.php [L,QSA]
您可以直接在 PHP 中使用
$_SERVER['REQUEST_URI']
。另一种尝试的路径是多个
部分(一个用于子目录,一个用于根目录)。但是,正如您所指出的,仅输入单个 PHP 文件并重定向通常更容易调试。
I think your problem has to do with RewriteBase and/or the ~file match. Did you try just using one base, and using that for your rules? Not totally sure what you are trying with your rules, but
RewriteEngine On
RewriteBase /
RewriteRule ^files/(.+) /file-handler.php?id=$1 [L,QSA]
RewriteRule !.(pdf|gif|jpg|png|css|swf|txt|php|ico|html|js|xml|flv)$ /index.php [L,QSA]
You can just use
$_SERVER['REQUEST_URI']
in PHP directly.The other path to try is multiple
<Directory>
sections (one for the subdir and one for the root).But, as you noted, just punting into a single PHP file and redirecting there is often easier to debug.
未经测试,但我认为它会起作用。
第一行:匹配 www.example.com/files/* 的任何请求。<上面列表中的文件类型>
第二行:将该匹配重定向到 file-handler.php?id=<来自上面的匹配>
第三行:匹配其他所有内容
Not tested, but I think it will work.
First line: match any requests for www.example.com/files/*.<file types in list above>
Second line: redirect that match to file-handler.php?id=<match from above>
Third line: match everything else
尝试这些规则:
第一个规则匹配 URI 路径的每个请求(没有上下文路径前缀,请参阅
RewriteBase
指令)以files/
开头。第二条规则匹配路径既不以files/
开头也不以列出的文件扩展名之一结尾的每个请求。Try these rules:
The first rule matches every request that’s URI path (without the contextual path prefix, see
RewriteBase
directive) starts withfiles/
. And the second rules matches every request that’s path does neither start withfiles/
nor ends with one of the listed file name extensions.