重写规则index.php?key = value

时间:2013-06-07 03:41:05

标签: .htaccess mod-rewrite

我的链接看起来像这样:

http://localhost/account/index.php?key=changepassword
http://localhost/account/index.php?key=changeemail

http://localhost/library/index.php?key=nemesis
http://localhost/library/index.php?key=books

我怎么能把它们改写成:

http://localhost/account/changepassword
http://localhost/account/changeemail

http://localhost/library/nemesis
http://localhost/library/books

1 个答案:

答案 0 :(得分:0)

通过httpd.conf启用mod_rewrite和.htaccess,然后将此代码放在.htaccess目录下的DOCUMENT_ROOT中:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(account)/(.+?)/?$ /$1/index.php?key=$2 [L,QSA,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(library)/(.+?)/?$ /$1/index.php?key=$2 [L,QSA,NC]