Url rewriting url重写如何重新确认url的一部分是数字或num+;话

Url rewriting url重写如何重新确认url的一部分是数字或num+;话,url-rewriting,Url Rewriting,我有一个问题来修复url重写代码 RewriteRule ^([aA-zZ0-9_-]+)/?([0-9]*)/?([A-Za-z0-9_-]*)/?([A-Za-z0-9_-]*)/?$ index.php?m=$1&r=$2&a=$3&b=$4 [QSA] 它可以很好地处理以下内容:(whit和whitout结尾“/) 问题是当gotoA以一个数字开始时,例如: Test/123gotoA 该报告: index.php?m=Test&r=123&

我有一个问题来修复url重写代码

RewriteRule ^([aA-zZ0-9_-]+)/?([0-9]*)/?([A-Za-z0-9_-]*)/?([A-Za-z0-9_-]*)/?$ index.php?m=$1&r=$2&a=$3&b=$4 [QSA]
它可以很好地处理以下内容:(whit和whitout结尾“/)

问题是当gotoA以一个数字开始时,例如:

Test/123gotoA 
该报告:

index.php?m=Test&r=123&a=gotoA&b=

如何修复它?

问题是您将
/
设置为可选。它是整个
/(xxx)
,应该是可选的

试试这个:

RewriteRule ^([aA-zZ0-9_-]+)(?:/(\d+))?(?:/([a-zA-Z\d_-]+))?(?:/([a-zA-Z\d_-]+))?/?$  index.php?m=$1&r=$2&a=$3&b=$4 [QSA]
如果我们将带有
Test/123gotoA
的原始正则表达式作为输入,则会发生以下情况:

#before first match
regex: |^([aA-zZ0-9_-]+)/?([0-9]*)/?([A-Za-z0-9_-]*)/?([A-Za-z0-9_-]*)/?$
input: |Test/123gotoA
# ^
regex: ^|([aA-zZ0-9_-]+)/?([0-9]*)/?([A-Za-z0-9_-]*)/?([A-Za-z0-9_-]*)/?$
input: |Test/123gotoA
# [aA-zZ0-9_-], once or more, captured: $1 is "Test"
regex: ^([aA-zZ0-9_-]+)|/?([0-9]*)/?([A-Za-z0-9_-]*)/?([A-Za-z0-9_-]*)/?$
input: Test|/123gotoA
# /?: a / is found
regex: ^([aA-zZ0-9_-]+)/?|([0-9]*)/?([A-Za-z0-9_-]*)/?([A-Za-z0-9_-]*)/?$
input: Test/|123gotoA
# [0-9], zero or more times, captured: $2 is "123"
regex: ^([aA-zZ0-9_-]+)/?([0-9]*)|/?([A-Za-z0-9_-]*)/?([A-Za-z0-9_-]*)/?$
input: Test/123|gotoA
# /?: no /, but satisified, since ? can match zero times
regex: ^([aA-zZ0-9_-]+)/?([0-9]*)/?|([A-Za-z0-9_-]*)/?([A-Za-z0-9_-]*)/?$
input: Test/123|gotoA
# [A-Za-z0-9_-], zero or more times, captured: $3 is "gotoA"
regex: ^([aA-zZ0-9_-]+)/?([0-9]*)/?([A-Za-z0-9_-]*)|/?([A-Za-z0-9_-]*)/?$
input: Test/123gotoA|
# /?: no /, but satisified, since ? can match zero times
regex: ^([aA-zZ0-9_-]+)/?([0-9]*)/?([A-Za-z0-9_-]*)/?|([A-Za-z0-9_-]*)/?$
input: Test/123gotoA|
# [A-Za-z0-9_-], zero or more times, captured: $4 is ""
regex: ^([aA-zZ0-9_-]+)/?([0-9]*)/?([A-Za-z0-9_-]*)/?([A-Za-z0-9_-]*)|/?$
input: Test/123gotoA|
# /?: no /, but satisified, since ? can match zero times
regex: ^([aA-zZ0-9_-]+)/?([0-9]*)/?([A-Za-z0-9_-]*)/?([A-Za-z0-9_-]*)/?|$
input: Test/123gotoA|
# $: satisified, since this is the end of input
regex: ^([aA-zZ0-9_-]+)/?([0-9]*)/?([A-Za-z0-9_-]*)/?([A-Za-z0-9_-]*)/?$|
input: Test/123gotoA|
# end of match: success
现在,对于重写的正则表达式,情况有所不同:

# before first match
regex: |^([aA-zZ0-9_-]+)(?:/(\d+))?(?:/([a-zA-Z\d_-]+))?(?:/([a-zA-Z\d_-]+))?/?$
input: |Test/123gotoA
# ^
regex: ^|([aA-zZ0-9_-]+)(?:/(\d+))?(?:/([a-zA-Z\d_-]+))?(?:/([a-zA-Z\d_-]+))?/?$
input: |Test/123gotoA
# [aA-zZ0-9_-], one or more times, captured: $1 is "Test"
regex: ^([aA-zZ0-9_-]+)|(?:/(\d+))?(?:/([a-zA-Z\d_-]+))?(?:/([a-zA-Z\d_-]+))?/?$
input: Test|/123gotoA
# Try and find /, then one or more digits, zero or one time: match, $2 is "123"
regex: ^([aA-zZ0-9_-]+)(?:/(\d+))?|(?:/([a-zA-Z\d_-]+))?(?:/([a-zA-Z\d_-]+))?/?$
input: Test/123|gotoA
# next group: a / must be found, but the next character is "g": backtrack, $2 is now "12"
regex: ^([aA-zZ0-9_-]+)(?:/(\d+))?|(?:/([a-zA-Z\d_-]+))?(?:/([a-zA-Z\d_-]+))?/?$
input: Test/12|3gotoA
# but here again the next character is not a /: the engine must backtrak again, until:
regex: ^([aA-zZ0-9_-]+)(?:/(\d+))?|(?:/([a-zA-Z\d_-]+))?(?:/([a-zA-Z\d_-]+))?/?$
input: Test|/123gotoA
# (?:/(\d+))? is still satified, as it is optional, and $2 is now ""
# Next group: /, followed by [a-zA-Z\d_-] one or more times, captures: $3 is "123gotoA"
regex: ^([aA-zZ0-9_-]+)(?:/(\d+))?(?:/([a-zA-Z\d_-]+))?|(?:/([a-zA-Z\d_-]+))?/?$
input: Test/123gotoA|
# Next group: same as second, but satisfied with an empty match: $4 is ""
regex: ^([aA-zZ0-9_-]+)(?:/(\d+))?(?:/([a-zA-Z\d_-]+))?(?:/([a-zA-Z\d_-]+))?|/?$
input: Test/123gotoA|
# an optional /: satisified
regex: ^([aA-zZ0-9_-]+)(?:/(\d+))?(?:/([a-zA-Z\d_-]+))?(?:/([a-zA-Z\d_-]+))?/?|$
input: Test/123gotoA|
# end of input: satisified
regex: ^([aA-zZ0-9_-]+)(?:/(\d+))?(?:/([a-zA-Z\d_-]+))?(?:/([a-zA-Z\d_-]+))?/?$|
input: Test/123gotoA|
# Match

更好的方法是编写几个重写规则,这样您的正则表达式就更简单了
# before first match
regex: |^([aA-zZ0-9_-]+)(?:/(\d+))?(?:/([a-zA-Z\d_-]+))?(?:/([a-zA-Z\d_-]+))?/?$
input: |Test/123gotoA
# ^
regex: ^|([aA-zZ0-9_-]+)(?:/(\d+))?(?:/([a-zA-Z\d_-]+))?(?:/([a-zA-Z\d_-]+))?/?$
input: |Test/123gotoA
# [aA-zZ0-9_-], one or more times, captured: $1 is "Test"
regex: ^([aA-zZ0-9_-]+)|(?:/(\d+))?(?:/([a-zA-Z\d_-]+))?(?:/([a-zA-Z\d_-]+))?/?$
input: Test|/123gotoA
# Try and find /, then one or more digits, zero or one time: match, $2 is "123"
regex: ^([aA-zZ0-9_-]+)(?:/(\d+))?|(?:/([a-zA-Z\d_-]+))?(?:/([a-zA-Z\d_-]+))?/?$
input: Test/123|gotoA
# next group: a / must be found, but the next character is "g": backtrack, $2 is now "12"
regex: ^([aA-zZ0-9_-]+)(?:/(\d+))?|(?:/([a-zA-Z\d_-]+))?(?:/([a-zA-Z\d_-]+))?/?$
input: Test/12|3gotoA
# but here again the next character is not a /: the engine must backtrak again, until:
regex: ^([aA-zZ0-9_-]+)(?:/(\d+))?|(?:/([a-zA-Z\d_-]+))?(?:/([a-zA-Z\d_-]+))?/?$
input: Test|/123gotoA
# (?:/(\d+))? is still satified, as it is optional, and $2 is now ""
# Next group: /, followed by [a-zA-Z\d_-] one or more times, captures: $3 is "123gotoA"
regex: ^([aA-zZ0-9_-]+)(?:/(\d+))?(?:/([a-zA-Z\d_-]+))?|(?:/([a-zA-Z\d_-]+))?/?$
input: Test/123gotoA|
# Next group: same as second, but satisfied with an empty match: $4 is ""
regex: ^([aA-zZ0-9_-]+)(?:/(\d+))?(?:/([a-zA-Z\d_-]+))?(?:/([a-zA-Z\d_-]+))?|/?$
input: Test/123gotoA|
# an optional /: satisified
regex: ^([aA-zZ0-9_-]+)(?:/(\d+))?(?:/([a-zA-Z\d_-]+))?(?:/([a-zA-Z\d_-]+))?/?|$
input: Test/123gotoA|
# end of input: satisified
regex: ^([aA-zZ0-9_-]+)(?:/(\d+))?(?:/([a-zA-Z\d_-]+))?(?:/([a-zA-Z\d_-]+))?/?$|
input: Test/123gotoA|
# Match