【问题标题】:Create a PHP page that redirects based on Query String or use .htaccess创建一个基于查询字符串重定向的 PHP 页面或使用 .htaccess
【发布时间】:2015-03-03 01:16:16
【问题描述】:

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

我在我的 wordpress 网站上创建了一个页面 /formsubmitredirect,它成功地从链接末尾提取了查询字符串 /formsubmitredirect?No 或 /formsubmitredirect?Yes 基于用户输入。

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

谢谢

【问题讨论】:

    标签: php wordpress .htaccess redirect


    【解决方案1】:

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

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

    根据我使用 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");
    

    【讨论】:

    • 您好,感谢您的回复,我会将这段代码放在我的 wordpress 网站的什么位置?
    • 通常在functions.php或插件中
    • 这会产生错误:无法修改标头信息 - 标头已发送
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      查询字符串在 $_GET() 数组中,所以你可以这样做 -

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

      【讨论】:

        【解决方案4】:

        你可以使用

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

        【讨论】:

          【解决方案5】:

          您可以使用基于您的 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 简单地触发此开关

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-07-21
            • 2018-11-20
            • 2016-04-25
            • 1970-01-01
            • 2013-10-22
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多