I have to obtain the last group of amounts from the random url, the url appears like:


a,b,c,d etc.. could be anything, amounts, letters and can also have dashes among

We're using kohana with this project so there's some additional rewrite rules in play but this is exactly what I've to date...

# Turn on URL rewriting
RewriteEngine On

# Installation directory
RewriteBase /site/

# Protect hidden files from being viewed
<Files .*>
    Order Deny,Allow
    Deny From All

# Protect application and system files from 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 any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]

I've attempted a couple of dozen designs, configurations, and investigated Google for any couple of hrs without any specific solutions.

I've been capable of getting the 123 simply by itself but never using the directory still attached, also after i have attempted a couple of designs I finish up inside a endless loop and obtain an apache error.

The finish result could be /directory/directory2/123


Your rule

RewriteRule ([0-9]*)$ index.php/listing/$1

is not helpful, since it does not change REQUEST_URI, therefore the PHP will not begin to see the rewritten index.php/listing/123, it'll begin to see the original /listing/foo-123. Should you add the [L] flag, it'll get into a loop, since the corresponding ReqeuestCond will still be true.

Typically you'd pass URL bits towards the script as parameters, for instance

RewriteRule ([0-9]*)$ index.php?listing=$1 [L]

However, within this form it won't work, because ([0-9]*)$ matches the empty string around the finish associated with a path, therefore it may cause two rewrites:

listing/foo-(123) → index.php?listing=123  # this is what you want ...
index.php()       → index.php?listing=     # ... but it gets rewritten
index.php()       → index.php?listing=     # no change so this is final

This occurs because all rewrite rules are examined right from the start after every rewrite (no matter the [L] flag).

Thus, you'll need a more specific rule

RewriteRule ^listing/[^/]*-([0-9]*)$ index.php?listing=$1 [L]

This creates its very own, however it would communicate with one last rule, so give a condition on that to avoid it looping

RewriteCond $0 !^/index.php($|/)  # $0 is what the RewriteRule matched
RewriteRule .* index.php/$0 [L]