apache - Using mod_rewrite how to Get the last 3 digits from a url -
i need last set of numbers random url, url looks like:
/directory/directory2/a-b-c-d-123
a,b,c,d etc.. can anything, numbers, letters have dashes in between
we using kohana project there additional rewrite rules in play have far...
# turn on url rewriting rewriteengine on # installation directory rewritebase /site/ # protect hidden files being viewed <files .*> order deny,allow deny </files> # protect application , system files being viewed rewriterule ^(?:application|modules|system)\b - [f,l] #my code attempts here rewritecond %{request_uri} dealership/listing/ rewriterule ([0-9]*)$ index.php/dealership/listing/$1 # allow files or directories exist displayed directly rewritecond %{request_filename} !-f rewritecond %{request_filename} !-d # rewrite other urls index.php/url rewriterule .* index.php/$0 [pt]
i have tried few dozen configurations, setups, , researched google few hours no specific answers.
i have been able 123 never directory still attached, when have tried few configurations end in endless loop , apache error.
the end result /directory/directory2/123
thanks!
your rule
rewriterule ([0-9]*)$ index.php/listing/$1
isn't useful, because doesn't change request_uri
, php won't see rewritten index.php/listing/123
, see original /listing/foo-123
. if add [l]
flag, go loop, because corresponding reqeuestcond
continue true.
typically pass url bits script parameters, example
rewriterule ([0-9]*)$ index.php?listing=$1 [l]
however, in form not work, because ([0-9]*)$
matches empty string on end of path, cause 2 rewrites:
listing/foo-(123) → index.php?listing=123 # want ... index.php() → index.php?listing= # ... gets rewritten index.php() → index.php?listing= # no change final
this happens because rewrite rules evaluated beginning after every rewrite (regardless of [l]
flag).
thus, need more specific rule
rewriterule ^listing/[^/]*-([0-9]*)$ index.php?listing=$1 [l]
this works on own, interact final rule, add condition on prevent looping
rewritecond $0 !^/index.php($|/) # $0 rewriterule matched rewriterule .* index.php/$0 [l]
Comments
Post a Comment