2014-04-04 51 views
4

這是我第一次嘗試.htaccess.htaccess重寫url獲取參數

這就是我想要的。

example.com/account/blitzen12 -> example.com/account/index.php?id=10&name=blitzen12 

我不想在重寫中包含id,這有可能嗎?

Note: id and name which is 10 and blitzen12 is retrive from the database. 

到目前爲止,這是我試過,但它沒有奏效。

Options +FollowSymLinks 
RewriteEngine On 

RewriteRule ^account/(.*)$ ./account/index.php?page=account&id=$1&name=$2 [L,NC] 

html代碼。

<a href="account/index.php?id=10&name=blitzen12">blitzen12</a> 

任何人都可以幫助我嗎?謝謝。

回答

9

的「10」或ID,是不是URL的一部分:

example.com/account/blitzen12 

所以你不能把它改寫成另一個URL,不能把它憑空。你要麼只是需要服務的頁面沒有「身份證」(將其拉出使用「名」的數據庫),或將其嵌入的URL沒有查詢字符串,是這樣的:

example.com/account/10/blitzen12 

那麼你就可以使用它來重寫它:

Options +FollowSymLinks 
RewriteEngine On 

RewriteRule ^account/([0-9]+)/(.*)$ ./account/index.php?page=account&id=$1&name=$2 [L,NC] 
+0

我試過你上面提到的,但它沒有奏效。 供參考我會編輯幷包含html以及.. – blitzen12

+0

是否可以重寫html響應?我不確定它是否被稱爲出站規則? – blitzen12

+2

@ blitzen12:mod_rewrite適用於_requests_到達服務器;它與HTML沒有任何關係。如果您希望HTML中的鏈接網址具有特定的形式,那麼_you_必須確保它們具有這種形式。 – CBroe