Php 如何在重写图案填充regexp中保留空字符串作为参数

Php 如何在重写图案填充regexp中保留空字符串作为参数,php,regex,.htaccess,url,Php,Regex,.htaccess,Url,在hatches重写条件下,我无法将空参数作为url传递 我的rewrite simple将所有参数splitter by/作为php GET传递: RewriteRule /?([A-Z.a-z0-9_-]*)/?([A-Z.a-z0-9_-]*)/?([A-Z.a-z0-9_-]*)/?([A-Z.a-z0-9_-]*)/?([A-Z.a-z0-9_-]*)/?([A-Z.a-z0-9_-]*)/?$ index.php?option1=$1&option2=$2&optio

在hatches重写条件下,我无法将空参数作为url传递

我的rewrite simple将所有参数splitter by/作为php GET传递:

RewriteRule /?([A-Z.a-z0-9_-]*)/?([A-Z.a-z0-9_-]*)/?([A-Z.a-z0-9_-]*)/?([A-Z.a-z0-9_-]*)/?([A-Z.a-z0-9_-]*)/?([A-Z.a-z0-9_-]*)/?$ index.php?option1=$1&option2=$2&option3=$3&option4=$4&option5=$5&option6=$6
但当我尝试此URL时:

---.com/fiction//204

我在php中获得这些get参数(注意url中的双
/
):

但是我想要

Array ( 
[option1] => fiction 
[option2] =>  
[option3] => 204
[option4] => 
[option5] => 
[option6] => )
我尝试向rexexp添加一个空字符串:
(^$|[A-Z.A-z0-9_-]*)
,但这不起作用


有什么想法吗?

您可能需要这6条规则,而不是您现有的规则:

RewriteRule /([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/?$ index.php?option1=$1&option2=$2&option3=$3&option4=$4&option5=$5&option6=$6
RewriteRule /([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/?$ index.php?option1=$1&option2=$2&option3=$3&option4=$4&option5=$5
RewriteRule /([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/?$ index.php?option1=$1&option2=$2&option3=$3&option4=$4
RewriteRule /([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/?$ index.php?option1=$1&option2=$2&option3=$3
RewriteRule /([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/?$ index.php?option1=$1&option2=$2
RewriteRule /([A-Z.a-z0-9_-]*)/?$ index.php?option1=$1
RewriteRule /$ index.php

但我不确定带有多个连续斜杠的URL是否适用于所有浏览器和服务器,我认为这通常不是一个好主意。

您可能需要以下6条规则,而不是您现有的规则:

RewriteRule /([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/?$ index.php?option1=$1&option2=$2&option3=$3&option4=$4&option5=$5&option6=$6
RewriteRule /([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/?$ index.php?option1=$1&option2=$2&option3=$3&option4=$4&option5=$5
RewriteRule /([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/?$ index.php?option1=$1&option2=$2&option3=$3&option4=$4
RewriteRule /([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/?$ index.php?option1=$1&option2=$2&option3=$3
RewriteRule /([A-Z.a-z0-9_-]*)/([A-Z.a-z0-9_-]*)/?$ index.php?option1=$1&option2=$2
RewriteRule /([A-Z.a-z0-9_-]*)/?$ index.php?option1=$1
RewriteRule /$ index.php

但我不确定带有多个连续斜杠的URL是否适用于所有浏览器和服务器,我认为这通常是个坏主意。

这些空值必须从
RewriteCond
中捕获,因为
RewriteRule
将所有多个
/
拆分为单个
//code>

这应该适合您:

RewriteCond %{REQUEST_URI} ^/([\w-]*)(?:/([\w-]*)(?:/([\w-]*)(?:/([\w-]*)(?:/([\w-]*)(?:/([\w-]*))?)?)?)?)?/?$
RewriteRule ^ index.php?option1=%1&option2=%2&option3=%3&option4=%4&option5=%5&option6=%6 [L,QSA]

此规则使第一个参数之后的每个参数都是可选的。

必须从
RewriteCond
捕获这些空值,因为
RewriteRule
将所有多个
/
剥离为单个
/

这应该适合您:

RewriteCond %{REQUEST_URI} ^/([\w-]*)(?:/([\w-]*)(?:/([\w-]*)(?:/([\w-]*)(?:/([\w-]*)(?:/([\w-]*))?)?)?)?)?/?$
RewriteRule ^ index.php?option1=%1&option2=%2&option3=%3&option4=%4&option5=%5&option6=%6 [L,QSA]
此规则使第一个参数之后的每个参数都是可选的