【问题标题】:PHP Dynamic Redirection based on Users' Clicks基于用户点击的PHP动态重定向
【发布时间】:2015-05-17 02:57:29
【问题描述】:

有什么办法可以在php中实现以下功能?

我想根据用户的特定用户 URL 重定向用户。

例如:

我有许多用户网址,如下所示:

http://example.com/test/user1
http://example.com/test/user2
http://example.com/test/user3
http://example.com/test/user4 

当每个用户点击他的url时,他会得到一个index.php文件http://example.com/test/index.php

然后这个index.php file 将根据他们的网址重定向这些用户。

所以重定向方案将是:

http://example.com/test/user1  ->  http://destination1.com
http://example.com/test/user2  ->  http://destination2.com
http://example.com/test/user3  ->  http://destination3.com
http://example.com/test/user4  ->  http://destination4.com

【问题讨论】:

  • 到目前为止你尝试过什么?
  • @DavidKmenta ,到目前为止我还没有尝试过任何东西,因为我不知道如何开始,因为我对 php 不太流利。
  • 你的第一步应该是看看htaccess
  • .htaccess 方法无法工作,因为这是一个 wordpress 网站,我无法在 htaccess 中呈现简码。这就是为什么我找到了一种在 PHP 中执行此操作的方法

标签: php wordpress redirect url-redirection


【解决方案1】:

你可以这样试试:

if($_SERVER['REQUEST_URI']=="/test/user1"){
header("Location:http://destination1.com");
}
else if($_SERVER['REQUEST_URI']=="/test/user2"){
header("Location:http://destination2.com");
}

【讨论】:

    【解决方案2】:

    如果没有 php 假设你在 apache 上

    在您网站的根目录中创建 .htaccess 并将其添加到那里

    RewriteEngine On
    Redirect 301 /test/user1 http://destination1.com
    Redirect 301 /test/user2 http://destination2.com
    Redirect 301 /test/user3 http://destination3.com
    Redirect 301 /test/user4 http://destination4.com
    

    或在 ngix 上

    location /test/user1 {  
    rewrite ^(.*)$ http://destination1.com redirect;  
    }
    location /test/user2 {
    rewrite ^(.*)$ http://destination2.com redirect;
    }
    location /test/user3 {
    rewrite ^(.*)$ http://destination3.com redirect;
    }
    location /test/user4 {
       rewrite ^(.*)$ http://destination4.com redirect;
    }
    

    了解有关重定向的更多信息 https://moz.com/learn/seo/redirection

    【讨论】:

      【解决方案3】:

      这里有一个简单的例子来说明如何做到这一点。如果是大量用户的情况,那么也许您应该将url->destination 关系保存到数据库中。您可以将此代码放在/test/index.php 文件中,但在任何输出之前。并且还在您主题的 functions.php 文件中,在 wp 动作和稍后触发的动作中。

      global $wp;
      
      $users = array(
          'test/user1'  =>  'http://destination1.com',
          'test/user2'  =>  'http://destination2.com',
          'test/user3'  =>  'http://destination3.com',
          'test/user4'  =>  'http://destination4.com',
      );
      
      if ( !empty( $users[ $wp->request ] ) ) wp_redirect( $users[ $wp->request ] );
      else {
          // this block of code is executed if user is not found
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-23
        • 2012-01-11
        • 1970-01-01
        相关资源
        最近更新 更多