我需要從一個隨機的網址,以獲取最後一組數字的獲得最後3個數字,網址是這樣的:使用mod_rewrite如何從一個url
/目錄/ directory2/ABCD-123
a,b,c,d等等。可以是任何東西,數字,字母但總是會有間破折號在
我們在這個項目中使用kohana,所以有一些額外的重寫規則在玩,但這是我的到目前爲止...
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /site/
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# 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]
我嘗試了幾十個配置,設置並研究了Google幾個小時而沒有具體答案。
我已經能夠自己得到123,但從來沒有目錄仍然附加,當我已經嘗試了一些配置,我最終在一個無限循環,並得到一個Apache錯誤。
最終結果將是/ directory/directory2/123
謝謝!
正則表達式是:'^/directory/directory2 /([^ \ - ] +) - ([^ \ - ] +) - ([^ \ - ] +) - )$',a到d將是1美元到4美元,最後3美元是5美元。 – 2011-02-23 18:49:39
感謝Paulo,除了我不需要任何其餘的url只有最後一位數字。 RewriteCond%{REQUEST_URI} /?dir1/dir2 /([a-zA-Z0-9] [ - ]可以讓一些匹配的工作通過觀察模式來消除循環, ])+,只有匹配時才匹配。 RewriteRule([0-9] *)$ ...只是獲取url中的最後一個數字。 – atwsKris 2011-02-23 19:08:00
@Paulo Scardine - 爲什麼不回答? – Xailor 2011-02-23 19:25:42