.htaccess用GET参数重写

时间:2016-03-13 13:55:05

标签: php .htaccess mod-rewrite

我想设置.htaccess文件以重新填写url,例如example.com/33到example.com?id=33(example.com/index.php?id=33)和example.com/Test到示例。 com?name = Test(example.com/index.php?name=Test)

我的htaccess文件:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ /index.php?id=$1 [QSA,L]

但是当我转到url example.com/33时,它会将我重定向到index.php,但是id参数不存在。请问哪里有问题?

2 个答案:

答案 0 :(得分:0)

试试这个

RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?id=$1 [L]

答案 1 :(得分:0)

有2个重写规则,一个用于数字(id),另一个用于name参数:

RewriteEngine On

# ignore all existing files and directories for rewriting     
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^(\d+)/?$ index.php?id=$1 [QSA,L]

RewriteRule ^(\w+)/?$ index.php?name=$1 [QSA,L]