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 使用mod_rewrite检查请求_URI_Apache_.htaccess_Mod Rewrite_Apache2 - Fatal编程技术网

Apache 使用mod_rewrite检查请求_URI

Apache 使用mod_rewrite检查请求_URI,apache,.htaccess,mod-rewrite,apache2,Apache,.htaccess,Mod Rewrite,Apache2,我试图用mod_rewrite限制请求的URI长度,但现在遇到了一些问题。 代码: 它工作正常,所有的请求_URI==lentgh(6)指向index.php,但我需要所有不匹配的请求_URI被禁止或重定向到error.html,以便执行 提前谢谢。 我使用的是Apache>2.4.4 UPD 我的意思是像这个例子: If REQUEST\u URI==len(6)指向index.php 如果请求\u URI!=len(6)重定向到另一个资源一种方法是创建另一个使用负前瞻的规则 RewriteE

我试图用mod_rewrite限制请求的URI长度,但现在遇到了一些问题。 代码:


它工作正常,所有的请求_URI==lentgh(6)指向index.php,但我需要所有不匹配的请求_URI被禁止或重定向到error.html,以便执行

提前谢谢。 我使用的是Apache>2.4.4

UPD 我的意思是像这个例子:
If REQUEST\u URI==len(6)
指向index.php
如果请求\u URI!=len(6)
重定向到另一个资源

一种方法是创建另一个使用负前瞻的规则

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9]{6})$
RewriteRule .* index.php [L]

RewriteCond  %{REQUEST_URI}  !^/index.php
RewriteRule .*  404.php
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(?!/[a-zA-Z0-9]{6}/?$)
RewriteRule ^ http://google.com [L,R=301]

RewriteCond %{REQUEST_URI} ^(/[a-zA-Z0-9]{6}/?$)
RewriteRule ^ index.php [L]
我为我的情况提出了一个更优雅(可能更快)的解决方案

RewriteEngine On

RewriteCond %{REQUEST_URI} [^/.]{7}
RewriteRule ^ - [F]

RewriteRule .* index.php [L]

如果请求URI不是从/index.php开始的,或者不是从根“/”开始的,并且长度>6-禁止,否则指向index.php

我真的不确定你在问什么。你的实际目标是什么?您希望它也匹配文件吗?还是所有不存在的文件?我问是因为我不知道如何分割逻辑。如果匹配的URI指向index.php,如果不匹配指向另一个资源(例如)“但我需要所有不匹配的请求\u URI被禁止或重定向到error.html”没有意义。你能给出一些重定向的例子吗,以及你的目录结构?为什么“没有意义”?好的。我展示了一个简单的例子:
If-REQUEST\u-URI==len(6)
指向index.php
If-REQUEST\u-URI!=len(6)
指向google.com我不知道为什么,但第一个regexp不适合我。如果我先搬走<代码>^(!/[a-zA-Z0-9]{6}/?$)然后就可以了。老实说,如果@undone的答案对你有用,就用它吧。它比我的好,而且会表现得更好。是的,我同意你的看法。
RewriteEngine On

RewriteCond %{REQUEST_URI} [^/.]{7}
RewriteRule ^ - [F]

RewriteRule .* index.php [L]