.htaccess的查询字符串重写重写、字符串、htaccess

2023-09-02 11:40:20 作者:我爱你若能倒着说

我有一个网址 /?测试,我想重写 /page.php?page=test

I have URLs like /?test that I want to rewrite to /page.php?page=test

我想这样的,但由于某种原因,这是行不通的。

I tried like this but for some reason it doesn't work.

RewriteRule ^?([a-z0-9-+]{3,20})/?$ /page.php?page=$1 [NC,QSA]

我是什么做错了吗?

What am I doing wrong?

推荐答案

查询字符串只能用的 的RewriteCond 指令。该 重写规则 模式仅针对 URL路径(在.htaccess文件,而每个目录preFIX的URL路径)。

The query string can only be tested with the RewriteCond directive. The RewriteRule pattern is only tested against the URL path (in .htaccess files the URL path without the per-directory prefix).

那么试试这个:

RewriteCond %{QUERY_STRING} ^[a-z0-9-+]{3,20}$ [NC]
RewriteRule ^$ /page.php?page=%0 [QSA]