Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.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
Php .htaccess mod_rewrite捕获所有卡在循环中_Php_Regex_Apache_.htaccess_Mod Rewrite - Fatal编程技术网

Php .htaccess mod_rewrite捕获所有卡在循环中

Php .htaccess mod_rewrite捕获所有卡在循环中,php,regex,apache,.htaccess,mod-rewrite,Php,Regex,Apache,.htaccess,Mod Rewrite,我有一个.htaccess文件,正在尝试重写以下内容: myserver.com/path1/path2/->myserver.com/?customer=path1&portal=path2 然后,对除文件、目录或符号链接以外的任何其他内容进行全面捕获,以转到服务器的根目录,例如myserver.com/ 我本以为myserver.com/?customer=path1&portal=path2会被检测为一个文件,即index.php,它是服务器的根目录,还是查询字符串将其丢弃 有谁能告诉我为

我有一个.htaccess文件,正在尝试重写以下内容:

myserver.com/path1/path2/->myserver.com/?customer=path1&portal=path2

然后,对除文件、目录或符号链接以外的任何其他内容进行全面捕获,以转到服务器的根目录,例如myserver.com/

我本以为myserver.com/?customer=path1&portal=path2会被检测为一个文件,即index.php,它是服务器的根目录,还是查询字符串将其丢弃

有谁能告诉我为什么这是无限循环和如何修复

RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} !443
RewriteRule ^(.*)?$ https://%{HTTP_HOST}/$1 [L,R=301]
RewriteRule ^([^/]*)/([^/]*)/$ /?customer=$1&portal=$2 [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ / [L,R=301]
编辑

我刚刚尝试了以下方法

RewriteRule ([^/]*)/$ https://%{HTTP_HOST}/?customer=$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ / [L,R=301]
现在

重定向到:

https://myserver.com/?customer=path1
http://myserver.com/
然而

https://myserver.com/path1/
重定向到:

https://myserver.com/?customer=path1
http://myserver.com/

那么为什么https会导致规则不匹配呢?

只有在尝试使用端口443时,才能通过第一个条件

然后,下面的RewriteCond似乎不能完全满足您的需要,您可以尝试以下方法:
RewriteRule([^/]*)/([^/]*)/$/?customer=$1&portal=$2[L,R]
。 所以基本上它只是删除字符串匹配的开头

您可以在这里的一个非常好的工具上试用:

[编辑]

尝试将您的问题隔离到一个简单的示例中:

.htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} !80
RewriteRule ^(.*)?$ https://%{HTTP_HOST}/$1 [L,R=301]

RewriteRule ([^/]*)/([^/]*)/$ /?customer=$1&portal=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ / [L,R=301]
index.php

<?php
print_r($_SERVER);
[QUERY\u STRING]=>customer=path1&portal=path3

您不应该使用重定向,因为查询字符串已经是您想要的了。 在本例中,重定向不会永远循环。

如下所示:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]

RewriteRule ^([^/]+)/([^/]+)/?$ ?customer=$1&portal=$2 [L,QSA]

RewriteRule . / [L,R=301]

在测试此更改之前,请确保清除浏览器缓存。

是否可以确保您尚未使用
RewriteRule^(.*)重定向?$https://%{HTTP\u HOST}/$1[L,R=301]
匹配
RewriteCond%{SERVER\u PORT}!443
?@antoni不太清楚你的意思。那些应该捕获任何非https的内容并将其设置为https,然后再次检查规则。为什么在重写后添加
R
标志,这是故意的吗?您应该保留相同的url并通过规则进行转换,但不要重定向,我认为这是导致循环的原因,因为我希望它以myserver.com/?customer=path1&portal=path2的形式到达服务器,但即使我直接使用此url,它也会无限循环。因此,最后一条规则必须是拾取它,但我不确定为什么作为RewriteCond%{REQUEST_FILENAME}-f规则应该停止它的进程,因为它会击中我的index.php,或者我需要指定index.php吗?
RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]

RewriteRule ^([^/]+)/([^/]+)/?$ ?customer=$1&portal=$2 [L,QSA]

RewriteRule . / [L,R=301]