Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache .htaccess根据查询字符串将URL重写为文件的未统一版本(如果存在)_Apache_.htaccess_Mod Rewrite - Fatal编程技术网

Apache .htaccess根据查询字符串将URL重写为文件的未统一版本(如果存在)

Apache .htaccess根据查询字符串将URL重写为文件的未统一版本(如果存在),apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,我想为URL中包含nomin的任何文件的未统一版本提供服务 例如,如果我请求: http://localhost/foo/bar.min.js?nomin 我应该收到文件: http://localhost/foo/bar.js 但是,我希望只有当文件存在时才会发生这种情况 这是我的.htaccess文件中的内容: RewriteCond %{QUERY_STRING} nomin RewriteCond %{REQUEST_URI} (.*)\.min(\..*) RewriteCond

我想为URL中包含
nomin
的任何文件的未统一版本提供服务

例如,如果我请求:

http://localhost/foo/bar.min.js?nomin
我应该收到文件:

http://localhost/foo/bar.js
但是,我希望只有当文件存在时才会发生这种情况

这是我的.htaccess文件中的内容:

RewriteCond %{QUERY_STRING} nomin
RewriteCond %{REQUEST_URI} (.*)\.min(\..*)
RewriteCond %{DOCUMENT_ROOT}/$1$2 -f
RewriteRule (.*)\.min(\..*) $1$2 [L,QSA,NC]
但是,它不起作用。如果删除检查文件是否存在的行:

RewriteCond %{DOCUMENT_ROOT}/$1$2 -f
它开始工作,但可能指向不存在的文件

我试过:

  • 删除斜杠
    RewriteCond%{DOCUMENT_ROOT}$1$2-f
  • 像这样使用
    REQUEST_URI
    重写cond%{REQUEST_URI}/$1$2-f(带斜杠和不带斜杠),尽管这没有多大意义
  • 匹配整个内容,只检查不包括“.min”的部分

我做错了什么?



附带问题:有没有办法更改我的
RewriteCond
RewriteRule
,这样我就不必写
(.*).min(\..*)
两次了?

这应该对你有用:

RewriteCond %{QUERY_STRING} ^nomin$ [NC]
RewriteCond %{REQUEST_URI} ^(.+)\.min(\.[^.]+)$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%1%2 -f
RewriteRule ^ %1%2 [L,NC]

使用
%1
%2
而不是
$1
$2
RewriteCond

捕获的值无效。在第二行中,使用
$1
$2
。他们不是不安吗?这就是为什么我要设置这两个变量。另外,我需要它在任何目录/子目录中工作。不,它们没有取消设置,因为
$1
$2
是在
重写规则
本身中设置的。是否
.htaccess
更改对它所在目录或任何子目录中的任何文件的请求?您编辑的解决方案工作正常。你能解释一下我的问题是什么吗?您是如何修复它的?正如我在回答中所写的,
使用%1、%2而不是$1、$2作为从RewriteCond捕获的值。关于
RewriteCond%{REQUEST_URI}
的另一个重要点是,它总是在完整的URI上工作,而不是像
RewriteRule
那样只在每个目录上工作。
RewriteCond %{QUERY_STRING} ^nomin$ [NC]
RewriteCond %{REQUEST_URI} ^(.+)\.min(\.[^.]+)$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%1%2 -f
RewriteRule ^ %1%2 [L,NC]