当{REQUEST_FILENAME}是现有目录或文件时,如何使mod_rewrite以静默方式重定向并将参数传递给索引

当{REQUEST_FILENAME}是现有目录或文件时,如何使mod_rewrite以静默方式重定向并将参数传递给索引,file,mod-rewrite,redirect,directory,File,Mod Rewrite,Redirect,Directory,简介: localhost/root/admin是一个现有文件夹 我希望/root/admin转到/root/index.php?url=admin,而不更改地址栏中的url 仅当请求的文件是图像时除外 这是我为localhost/root中的.htaccess文件编写的,用于尝试执行所有这些操作: RewriteEngine On RewriteBase /root/ RewriteCond %{REQUEST_URI} !index\.php$ RewriteCond %{REQUEST_

简介:

localhost/root/admin是一个现有文件夹
我希望/root/admin转到/root/index.php?url=admin,而不更改地址栏中的url

仅当请求的文件是图像时除外

这是我为localhost/root中的.htaccess文件编写的,用于尝试执行所有这些操作:

RewriteEngine On
RewriteBase /root/

RewriteCond %{REQUEST_URI} !index\.php$
RewriteCond %{REQUEST_URI} !(.*\.(png|jpg|gif))$ [NC]
RewriteRule (.*) index.php?url=$1 [QSA,L]
这样做是将localhost/root/admin重定向到localhost/root/index.php,并将地址栏中的url更改为localhost/root/admin/?url=admin

只有在“admin”上添加一个尾随斜杠(/)时,它才不会这样做

我知道,在我的mac笔记本电脑上,这个mod_重写代码完全按照它的意思执行,但只有在“Sites”文件夹中运行时,而不是在localhost htdocs文件夹中运行


我能做些什么来解决这个问题呢?

我想你只是错过了“PT”标志来阻止实际的重定向。PT(Passthrough)意味着Apache重定向内部服务的资源,但浏览器看到相同的URL。(有关更多详细信息,请参阅Apache wiki:)


注意,唯一的更改是添加标志。我在我的服务器上进行了测试,发现了我认为您需要的行为。

与以前一样的问题,即使使用了PT标志。我在“localhost/root/index.php”中有一个index.php文件,在“localhost/root/admin/index.php”中有另一个index.php文件。如果我转到“localhost/root/admin”(不是localhost/root/admin/),它会像应该的那样从“localhost/root”向我发送信息,但会将我的地址栏更改为“localhost/root/admin/?url=admin”。最重要的是,使用您的代码,“localhost/root/admin/index.php”根本不会重定向我,但我希望每个请求都转到“localhost/root/index.php”,图像除外。有没有可能是localhost干扰了我的目标?嗯,你能发布你当前的.htaccess文件吗?再次检查这里是否没有.htaccess目录(并且在apache配置中没有其他重写规则)。
RewriteEngine On
RewriteBase /root/

RewriteCond %{REQUEST_URI} !index\.php$
RewriteCond %{REQUEST_URI} !(.*\.(png|jpg|gif))$ [NC]
RewriteRule (.*) index.php?url=$1 [PT,QSA,L]