2014-04-04 50 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