переписывание url, как восстановить, когда часть url числовая или num + words - PullRequest
0 голосов
/ 14 января 2012

У меня есть проблема, чтобы исправить этот код перезаписи 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]

, он хорошо работает с этими: (с окончанием "/"), "1004 *

Test/1234/gotoA/gotoB/
Test/1234/gotoA/
Test/1234/
Test/gotoA/gotoB/

проблема в том, когдаgotoA начать с примера числа:

Test/123gotoA 

, которые возвращаются:

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

как я могу это исправить?

1 Ответ

1 голос
/ 14 января 2012

Проблема в том, что вы делаете / необязательным. Это целое значение /(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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...