如何重写“something/test?id=test”到“test.php?id=test”在 .htaccess 中

发布于 2025-01-11 01:22:33 字数 217 浏览 1 评论 0原文

如何将 something/test?id=test 重写为 .htaccess 中的 test.php?id=test

RewriteRule ^v1/games/list?sortToken=(.*)$ /juicy.php?id=$1 [QSA,NC,L]

没有任何作用!

How to rewrite something/test?id=test to test.php?id=test in .htaccess

RewriteRule ^v1/games/list?sortToken=(.*)$ /juicy.php?id=$1 [QSA,NC,L]

Nothing works!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蘸点软妹酱 2025-01-18 01:22:33
RewriteRule ^v1/games/list?sortToken=(.*)$ /juicy.php?id=$1 [QSA,NC,L]

RewriteRule 模式仅与 URL 路径匹配,这特别排除了查询字符串。要匹配查询字符串,您需要使用附加条件并与QUERY_STRING服务器变量进行匹配。

如何将 something/test?id=test 重写为 test.php?id=test

但是,问题中给出的示例与您的 < 隐含的重写有很大不同代码>RewriteRule尝试?

要重写此示例,您可以使用如下内容:

Options -MultiViews

RewriteEngine On

RewriteRule ^[^/]+/(test)$ $1.php [L]

上面将在内部将 //test?id=test 重写为 test.php?id=test 。默认情况下会传递查询字符串,因此这会匹配任何查询字符串。

但是,要将 /v1/games/list?sortToken= 重写为 /juicy.php?id= (如您的“尝试”所暗示的那样) "),您需要执行类似以下操作:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^sortToken=([^&]*)
RewriteRule ^v1/games/list$ juicy.php?id=%1 [NE,L]
RewriteRule ^v1/games/list?sortToken=(.*)$ /juicy.php?id=$1 [QSA,NC,L]

The RewriteRule pattern matches against the URL-path only, which notably excludes the query string. To match the query string you need to use an additional condition and match against the QUERY_STRING server variable.

How to rewrite something/test?id=test to test.php?id=test

However, the example given in the question is quite different to the rewrite implied by your RewriteRule attempt?

To rewrite this example, you could use something like this:

Options -MultiViews

RewriteEngine On

RewriteRule ^[^/]+/(test)$ $1.php [L]

The above will internally rewrite /<something>/test?id=test to test.php?id=test. The query string is passed through by default, so this matches any query string.

However, to rewrite /v1/games/list?sortToken=<something> to /juicy.php?id=<something> (as implied by your "attempt"), you would need to do something like the following instead:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^sortToken=([^&]*)
RewriteRule ^v1/games/list$ juicy.php?id=%1 [NE,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文