2011-05-16 38 views
1

我需要打開一個查詢字符串是這樣的:htaccess的重寫規則

http://localhost/view.php?id=12345

進入這個:

http://localhost/12345

但是,我還計劃有一個字符串在搜索引擎URL的結尾處可以使用,如下所示:

http://localhost/12345/NameOfTheSkin

有點像Wowhead的是如何做好自己的URL:

http://www.wowhead.com/item=49623/shadowmourne 

注意如何更改字符串「影之哀傷」到任何東西,它仍然指向項ID 49623

基本上,我需要結束字符串被忽略。

任何幫助表示讚賞,歡呼聲。 :)

回答

4
RewriteRule ^/(\d+)/?.*$ /view.php?id=$1 [L] 

這將改寫http://host/1234/some-texthttp://host/1234http://host/view.php?id=1234

詳細說明:

^ -- matches at the start of the string 
( -- start match group 
\d+ -- match one or more digits 
) -- end match group 
/? -- match 0 or 1 slash 
.* -- match any character 0 or more times 
$ -- matches at the end of the string 

訪問regular-expressions.info爲正則表達式的完整指南。

+0

爲什麼會這樣?你能包括更多的細節/解釋嗎? – Joe 2011-05-16 17:59:04

+0

@Joe重寫規則的第一部分'^ /(\ d +)/?.*$'是一個正則表達式,它在查詢字符串模式'/ xxx/?.*'內提取數字'(\ d +)'。 '/?'試圖匹配一個斜線,'。*'匹配到'$'這一行的末尾。然後插入到view.php的請求中,作爲'$ 1'表示由第一個括號(數字)匹配的部分。 – 2011-05-16 18:07:50

+2

@Joe這是更新的答案。 – 2011-05-16 18:14:30