basic mod_rewrite / regexp question: Split ID with dash
I'm trying to do some htaccess redirects for cleaner URL's, but I failed to do.
Here is my intend:
/kitap/blah => kitap.php?a=blah
/kitap/blah-123 => kitap.php?a=blah&b=123
This is my current code.
RewriteRule ^(kitap|yazar)/?([^/\.]+)?(-([0-9]+))?/?$ $1.php?a=$2&b=$3 [QSA,L,NC]
Currently it producing this:
/kitap/blah => kitap.php?a=blah
/kitap/blah-123 => kitap.php?a=blah-开发者_开发百科123
The word "blah" can include Unicode characters, like ç,ö,ğ,ş etc. (This means [a-z0-9_] will not work in my situation)
How can I accomplish my intended result? I tried various things but no success. Thanks.
I ended up with a "hybrid" solution from Alin and battal's comments.
RewriteRule ^(kitap|yazar)/?([^/\.-]+)?(-([0-9]+))?/?$ $1.php?a=$2&b=$4 [QSA,L,NC]
And it works. Thanks!
RewriteRule ^(kitap|yazar)/?([^/\.-]+)?(-(\d+))?/?$ $1.php?a=$2&b=$4 [QSA,L,NC]
or
RewriteRule ^(kitap|yazar)/?(\p{L}+)?(-(\d+))?/?$ $1.php?a=$2&b=$4 [QSA,L,NC]
$4
, not $3
unless you intend to use the dash -
, if you use $3
, $_GET['b']
will be equal to -123
. Every pair of parantheses counts, so that $3
is -123
, whereas $4
is 123
.
\p
means a unicode character, and {L}
means a letter. You can see available modifiers at PHP PCRE regex - Unicode character properties. To use them, your version of PHP must be compiled with --enable-unicode-properties
.
精彩评论