Apache htaccess 重写 url

Apache htaccess rewrite url

我正在尝试更新我的网站 URL 结构,但面临 htaccess 重写规则的问题。

我的老 url 是这样的: http://www.example.com/index-test.php?page=5

我的目标是将其重写为如下内容:http://www.example.com/page/5

这样当我访问 http://www.example.com/page/5 时它会将其转发到索引-test.php.

为此我写了这两条规则,但它不起作用

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

RewriteRule ^page/?$  index-test.php [NC,L]
#RewriteRule ^page/([^/d]+)/?$ index-test.php?page= [L,QSA]

不知道我的 htaccess 规则有什么问题。

您要查找的正则表达式是:

RewriteRule ^page/([0-9]+)$  /index-test.php?page= [L]

您需要在“/”之后用一组() 捕获页面id。然后在重写中,你必须使用 $1 (链接到捕获的 id)。

注意:如果你想在没有任何 id 的情况下访问 http://www.example.com/page/,这一行将不起作用,如果你愿意,你可以使用:

RewriteRule ^page/([0-9]+)*$  /index-test.php?page= [L]