gpt4 book ai didi

php - 无法保留 $_GET 变量

转载 作者:行者123 更新时间:2023-12-04 05:38:01 27 4
gpt4 key购买 nike

我想从/gallery/X 重定向到 gallery.php?category=X

但是当我真正转到地址时,我的旧 $_GET 变量“类别”被转换为以下形式:

$_GET['category'] = "X.php/X"   

这是我的 .htaccess 的样子:
RewriteBase /

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^gallery/(.*)$ /gallery.php?category=$1 [L]

我在 RegEx 方面非常糟糕,对 .htaccess 几乎一无所知。我一直在尝试使用这个重写规则来保留 $_GET 变量,但我所做的一切似乎都不起作用。我应该在这里做什么?

谢谢!

最佳答案

移动你的规则:

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^gallery/(.*)$ /gallery.php?category=$1 [L]

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

另外,添加 QSA为您的行动, [L,QSA] .这会在重定向期间附加查询字符串,因此它应该保留您以前的数据。

说明:

您的重写需要按特定顺序进行,因为 [L]选项就像向服务器发出一个全新的请求。所以,当您请求 gallery/X进来了,把你的请求改写为 /gallery.php?category=X .当服务器请求此页面时,它匹配您的第一条规则,这意味着它被视为 X.php/X然后作为额外的 $1 返回到原始请求.

听起来令人困惑,但我认为这就是正在发生的事情。

关于php - 无法保留 $_GET 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11661576/

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