创建一个基于查询字符串重定向的 PHP 页面或使用 .htaccess


Create a PHP page that redirects based on Query String or use .htaccess

我已经购买了重力表单wordpress插件,但我遇到了一个问题,即它无法根据用户的输入将用户重定向到特定的URL。但是,开发人员确实告诉我,它具有重定向到页面并根据输入引入查询字符串的功能。我需要做的就是在页面上使用 PHP 根据查询字符串重定向到另一个 URL。

我已经在我的wordpress网站上创建了一个页面/formsubmitredirect,它成功地从链接/formsubmitredirect的末尾拉入查询字符串?否/表单提交重定向?是,基于用户输入。

如何让此页面根据这些查询字符串重定向?

谢谢

问题的答案取决于您如何获取查询字符串。您是在页面模板中还是在简码函数中执行此操作?

如果其中一个是真的,那么主要问题是,如果你在加载带有代码的页面之前已经将标题加载到 html 中,那么 php 会抱怨标题已经发送到浏览器并且不会做重定向!

根据我对wordpress的经验,最好的选择是挂接到template_redirect操作,检查那里的查询变量并在wordpress加载任何模板之前重定向。例如:

function my_redirect(){
    if(isset($_GET["yourvariable"]) && !empty($_GET["yourvariable"])){
        do your redirect here with wp_redirect()
        exit; //prevent wordpress continuing to load templates
    }
}
add_action("template_redirect","my_redirect");

在进行任何输出之前,请更改位置标头:

<?
header('Location: newloc.php'); // change to your liking

查询字符串位于$_GET()数组中,因此您可以执行此操作 -

if($_GET['formsubmitredirect '] == 'yes') {
   header("Location: newpage");
   exit();
} else {
   header("Location: somewhereelse");
   exit();
}
您可以使用

$_SERVER['QUERY_STRING']获取当前查询字符串,并可以使用 if 条件轻松重定向页面...

您可以使用基于 GET 参数的开关

switch ($_GET['location']) {
    case 'index':
        header("Location: index.php");
        break;
    case 'news':
        header("Location: news.php");
        break;
    default:
        header("Location: home.php");
        break;
}

现在,您可以使用 page.php?location=index 简单地触发此开关