gpt4 book ai didi

.htaccess - 使用 .htaccess 处理两个查询字符串

转载 作者:行者123 更新时间:2023-12-01 12:49:41 24 4
gpt4 key购买 nike

我正在尝试为具有两个查询字符串参数的页面重写 url。并根据这些参数值(设置或不设置),在页面上显示不同的内容。

example.com/fotograf.php?aid=10
example.com/fotograf.php?aid=10&fid=5

上面的 URL 是未重写的示例。我只想让他们这样

example.com/fotograf/10/
example.com/fotograf/10/5/

第一个 URL 链接到包含照片的相册,第二个 URL 链接到该相册中的照片。

.htaccess 中,我只希望能够使用上面的干净 URL 访问它们。之后,我将检查脚本顶部的 URL,如果它不干净,则使用我编写的函数将其重定向到干净的(通过使用 preg_match 和 preg_split 获取值并使用 header 函数重定向)。

到目前为止,我已经尝试过这条规则(但无法查看相册):

RewriteRule ^fotograf/([0-9-/]+)/([0-9]+)/?$ fotograf.php?aid=$1&fid=$2 [L]

然后,这个(总是重定向到相册):

RewriteRule ^fotograf/([0-9-/]+)/?$ fotograf.php?aid=$1 [L]

最后,这个(帮助我通过像 example.com/fotograf/10/?fid=5 这样的 URL 查看照片):

RewriteCond %{QUERY_STRING} ^fid=(.*)$
RewriteRule ^fotograf/(.*)/$ fotograf.php?aid=$1&fid=%1

但我不想在 URL 中看到任何问号或符号。

不知何故,我需要检查设置了哪些(哪些)并相应地重定向,但我只是不知道如何实现这一点。

我该怎么做?

提前致谢。

最佳答案

您需要让您的应用程序生成您想要的 URL,因此采用以下形式:

example.com/fotograf/10/
example.com/fotograf/10/5/

并且遵循重写规则将确保,它将到达您的 php:

RewriteEngine On

RewriteRule ^fotograf/([0-9]+)/([0-9]+)/?$ fotograf.php?aid=$1&fid=$2

RewriteRule ^fotograf/([0-9]+)/?$ fotograf.php?aid=$1

mod_rewrite 无法重写 HTML 页面中的 URL...

关于.htaccess - 使用 .htaccess 处理两个查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13439684/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com