2010-02-23 22 views
1

我試圖修改我的網頁,我目前使用QueryStrings來顯示網頁,並希望將網站網址更改爲「搜索引擎友好」的網頁。正則表達式爲.htaccess文件

我在正則表達式新手,也.htaccess文件,我想從

http://someaddress.bla/index.php?lang=en&rootCategory=$rootCaty&leafCategory=$leafCat 

更改URL來

http://someaddress.bla/en/$rootCat/$sub-category/$sub-category2/.../$leafCat 

類別在數據庫中存儲遞歸如此,我必須在URL中顯示每個類別級別。但我需要的只是網址中的最後一個類別。爲實例的類可以是根類,如:

http://someaddresss.bla/en/Company 

當前正在從該URL顯示:

http://someaddress.bla/?lang=en&RootCategory=Company 

,或者它可以是一個子類,如:

http://someaddress.bla/en/Company/AboutUs 

目前顯示的網址爲:

http://someaddress.bla/?lang=en&RootCategory=Company&LeafCategory=AboutUs 

或者可以是如下的葉:

http://someaddress.bla/en/Company/AboutUs/Staff/.../Steve其中「史蒂夫」是遞歸葉。 此頁面當前從URL顯示:

http://someaddress.bla/?lang=en&RootCategory=Company&LeafCategory=Steve 

我不能寫正則表達式來處理這種情況,任何幫助將不勝感激。謝謝!

+0

你有沒有考慮mod_rewrite所有的404請求到一個PHP文件,並在該文件中執行你的正則表達式? – 2010-02-23 02:00:18

+0

我不明白你在說什麼「mod_rewrite所有的404請求到一個單一的PHP文件」,但我有幾個PHP文件,我可以檢查內容是否丟失或不使用PHP。 我打算將正則表達式更改爲 「RewriteRule^/?([^ /] +)/([^ /] +)/?$ someFile.php?lang = $ 1&RootCategory = $ 2 RewriteRule ^/?([^ /] +)/([^ /] +)(/.+)?/([^ /] +)/?$ someFile.php?lang = $ 1&RootCategory = $ 2&LeafCategory = $ 4「 where I處理someFile.php中的數據 – fsonmezay 2010-02-23 15:41:42

回答

2
RewriteRule ^/?([^/]+)/([^/]+)/?$ /?lang=$1&RootCategory=$2 
RewriteRule ^/?([^/]+)/([^/]+)(/.+)?/([^/]+)/?$ /?lang=$1&RootCategory=$2&LeafCategory=$4 
+0

謝謝,它對我很好 – fsonmezay 2010-02-23 15:34:33

0

您可以通過錨固$(字符「字符串的結束」)匹配在URL中的最後一項:

([^/]+)$ 

將匹配和捕捉到的最後一組在URL中的字符不是一個/字符。