.htaccess中的RewriteRule Pattern否定会导致重定向过多

时间:2016-12-04 03:48:54

标签: apache .htaccess mod-rewrite

.htaccess的{​​{1}}到HTTP 302将每个请求重定向到/mydir/时,应该使用哪些RewriteRule?

http://www.example.com/mydir/output.htaccess的当前内容:

/mydir/

输入网址:

RewriteEngine On RewriteBase "/mydir/" RewriteRule !"^output$" "output" [R]

http://www.example.com/mydir/input1

http://www.example.com/mydir/input2

预期输出:

http://www.example.com/mydir/input3

http://www.example.com/mydir/output

http://www.example.com/mydir/output

实际输出:

http://www.example.com/mydir/output

ERR_TOO_MANY_REDIRECTS

ERR_TOO_MANY_REDIRECTS

3 个答案:

答案 0 :(得分:0)

RewriteEngine On
RewriteBase "/mydir/"
RewriteRule "^output$" "-" [L]
RewriteRule ".*" "output" [R]
# Further rules are not processed.

我不知道为什么否定不起作用,但原来的RewriteRule可以被上面的这2个替换。这样,如果URL已经是预期输出(请参阅dash替换和L flag),第一个RewriteRule将停止整个重写过程,以避免重定向循环。否则,第二个RewriteRule会将所有内容重写为预期的输出URL。如果应该避免停止整个重写过程,则可以仅跳过无条件(第二个)RewriteRule,因此处理每个进一步的规则。要实现此目的,请使用以下S flag

RewriteEngine On
RewriteBase "/mydir/"
RewriteRule "^output$" "-" [S=1]
RewriteRule ".*" "output" [R]
# Further rules are processed as well.

答案 1 :(得分:0)

您在规则上错过了L标记,但它太宽泛了;它应该只匹配发送到服务器的实际URL。我怀疑该请求的最终目的地是名为output的无扩展名文件,但这是您的规则所坚持的。试试这个:

RewriteEngine On
RewriteBase /mydir/
RewriteCond %{THE_REQUEST} !^\S++\s++/mydir/output\s
RewriteRule ^ output [NS,R,L]
# rules after this should now be processed

答案 2 :(得分:-1)

您正在检查您的网址是否未以output开头,而且情况总是如此。将规则更改为:

RewriteEngine On
RewriteRule !^/mydir/output$ /mydir/output [R]