使用QUERY_STRING重写网址

时间:2019-05-03 18:12:40

标签: .htaccess url-rewriting

我想重写http://example.com/test/index.php?id=123http://example.com/test/item/123/http://example.com/test/index.php?id=123重定向到人性化的网址。
我不知道我的htaccess有什么问题:

Options +FollowSymLinks -MultiViews

RewriteEngine On    
RewriteBase /test/

RewriteCond %{QUERY_STRING} ^id=([a-z0-9]+)$ [NC]
RewriteRule ^/item/%1/? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^item/([a-z0-9]+)/$ index.php?id=$1 [L]

1 个答案:

答案 0 :(得分:1)

您可以在/test/.htaccess文件中使用以下RewriteRule

Options +FollowSymLinks -MultiViews
RewriteEngine on

#redirect old url to the new one
RewriteCond %{THE_REQUEST} /test/index\.php\?id=([^\s]+) [NC]
RewriteRule ^ /test/item/%1? [L,R=301]
#internally map new url to the old one
RewriteRule ^item/(.+)/?$ /test/index.php?id=$1 [L,NC]