使用RewriteRule为index.php提供参数


Giving parameters to index.php with RewriteRule

我有一个重定向器,它将url中/之后的所有内容作为参数提供给隐藏的index.php(然后将js的window.location用户重定向到另一个域,主机不支持.htaccess的外部重定向),但我丢失了代码。每个文件(index.php,.htaccess)都在/storage文件夹中
.htaccess是这样的,但我想不通:

RewriteRule ^(.*) /?$1 [R,L]

这是一个无限循环的重定向。

它的工作原理就像进入http://storage.mysite.com/file.png将打开http://storage.mysite.com/?file.png一样
我试图避免在.htaccess中直接调用index.php,因为它重定向为:

<?php 
   echo "
       <script>
        window.location='http://otherdomain.com/12345678".$_SERVER['REQUEST_URI']."'
       </script> 
   "; // note there's no slash after the number, the REQUEST_URI had it
?>

这样做的正确方法是什么?

听起来这就是您所需要的:

RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)$ index.php

在PHP中,$_SERVER['REQUEST_URI']仍然是原始请求的URI /file.png

去掉R标志将修复循环。此外,不需要添加请求URI作为GET参数,如下所述
R标志意味着新地址/?file.png被发送到浏览器,并且浏览器随后对该URI进行新的请求
删除R标志意味着Apache在不通知浏览器的情况下为新文件index.php提供服务。

这意味着尽管index.php正在被解析,但是请求URI仍然是/file.png

RewriteCond %{REQUEST_URI} !^/index.php意味着如果请求是针对/index.php的,则不重写。

我已经测试过了,它是有效的。如果你有任何问题,请评论。

*的意思是"0或更多"。如果您只想重定向(如果至少有一个),那么您应该使用+