简单的重定向apache无限循环

时间:2014-02-17 08:26:49

标签: apache .htaccess

我在Windows 7下使用xampp。我已将.htaccess放在%XAMPP_ROOT%/ htdocs中。 .htaccess包含以下内容:

Redirect / http://localhost/test.php/

在浏览器的地址字段中输入http://localhost后,我发现了一个infinte循环错误 http://localhost/test.php/test.php/test.php/test.php/test.php/test.php/test.php/test.php/test.php/test.php/test.php/test.php/test.php/test.php/test.php/test.php/test.php/test.php/test.php/test.php/。但我预计我会被重定向到http://localhost/test.php/

2 个答案:

答案 0 :(得分:1)

Redirect directive匹配以提供的网址开头的所有内容:

  

然后以URL-Path开头的任何请求都会在目标网址的位置向客户端返回重定向请求。

这意味着/test.php/将匹配包括/在内的所有请求。此外,斜杠后的所有内容都将附加到目标URL。这就是你获得递归URL的原因。

您必须使用RedirectMatch仅匹配根路径RedirectMatch ^/?$ http://localhost/test.php/

//Gloabal middleware
app.get('/*',function(request,response,next){
  response.header('Access-Control-Allow-Origin' , 'http://domain' );
  response.header('Access-Control-Allow-Credentials', true);
  next();
});

感谢anubhava了解该解决方案。

答案 1 :(得分:0)

您应该使用RedirectMatch来避免使用正则表达式的这种情况:

RedirectMatch ^/?$ http://localhost/test.php/