mod_rewrite - 重定向到几个不同的 PHP 文件

发布于 2024-09-28 06:37:37 字数 698 浏览 1 评论 0原文

我希望对 /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 技术交流群。

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

发布评论

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

评论(5

优雅的叶子 2024-10-05 06:37:37

您使用过虚拟主机吗? <虚拟主机>部分应该有“RewriteOptions Inherit”

Have you used Virtual Hosts? < VirtualHost> section should have "RewriteOptions Inherit"

爱,才寂寞 2024-10-05 06:37:37

好吧,我没有解决这个问题,但找到了一个解决方法 - 我只是重定向到一个 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...

凡间太子 2024-10-05 06:37:37

我认为你的问题与 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.

城歌 2024-10-05 06:37:37
RewriteCond ^files/(.*)\.(pdf|gif|jpg|png|css|swf|txt|php|ico|html|js|xml|flv)$
RewriteRule file-handler.php?id=$1 [L,QSA]
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

未经测试,但我认为它会起作用。

第一行:匹配 www.example.com/files/* 的任何请求。<上面列表中的文件类型>

第二行:将该匹配重定向到 file-handler.php?id=<来自上面的匹配>

第三行:匹配其他所有内容

RewriteCond ^files/(.*)\.(pdf|gif|jpg|png|css|swf|txt|php|ico|html|js|xml|flv)$
RewriteRule file-handler.php?id=$1 [L,QSA]
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

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

携余温的黄昏 2024-10-05 06:37:37

尝试这些规则:

RewriteRule ^files/(.+) file-handler.php?id=$1 [L,QSA]
RewriteCond %{REQUEST_URI} !\.(pdf|gif|jpg|png|css|swf|txt|php|ico|html|js|xml|flv)$
RewriteRule !^files/ /index.php?page=%{REQUEST_URI} [L]

第一个规则匹配 URI 路径的每个请求(没有上下文路径前缀,请参阅 RewriteBase 指令)以 files/ 开头。第二条规则匹配路径既不以 files/ 开头也不以列出的文件扩展名之一结尾的每个请求。

Try these rules:

RewriteRule ^files/(.+) file-handler.php?id=$1 [L,QSA]
RewriteCond %{REQUEST_URI} !\.(pdf|gif|jpg|png|css|swf|txt|php|ico|html|js|xml|flv)$
RewriteRule !^files/ /index.php?page=%{REQUEST_URI} [L]

The first rule matches every request that’s URI path (without the contextual path prefix, see RewriteBase directive) starts with files/. And the second rules matches every request that’s path does neither start with files/ nor ends with one of the listed file name extensions.

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