基于用户的PHP动态重定向';咔哒声
有没有办法在php中实现以下功能 我想根据用户的特定用户URL重定向用户 例如: 我有许多用户URL,如下所示:基于用户的PHP动态重定向';咔哒声,php,wordpress,redirect,url-redirection,Php,Wordpress,Redirect,Url Redirection,有没有办法在php中实现以下功能 我想根据用户的特定用户URL重定向用户 例如: 我有许多用户URL,如下所示: http://example.com/test/user1 http://example.com/test/user2 http://example.com/test/user3 http://example.com/test/user4 当每个用户点击他的url时,他将在http://example.com/test/index.php 这个index.php文件将根据这些用户
http://example.com/test/user1
http://example.com/test/user2
http://example.com/test/user3
http://example.com/test/user4
当每个用户点击他的url时,他将在http://example.com/test/index.php
这个index.php文件将根据这些用户的URL重定向这些用户
因此,重定向方案将是:
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
您可以这样尝试:
if($_SERVER['REQUEST_URI']=="/test/user1"){
header("Location:http://destination1.com");
}
else if($_SERVER['REQUEST_URI']=="/test/user2"){
header("Location:http://destination2.com");
}
如果没有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;
}
了解有关重定向的更多信息
下面是一个简单的例子,说明如何做到这一点。如果是大量用户的情况,那么您可能应该将url->destination
关系保存到数据库中。您可以将此代码放在/test/index.php
文件中,但放在任何输出之前。在主题的functions.php
文件中,在wp
action和稍后激发的actions中
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
}
到目前为止,你尝试了什么?@DavidKmenta,到目前为止,我还没有尝试过任何东西,因为我不知道如何开始,因为我的php不是很流利。你的第一步应该看看htaccess。htaccess方法无法工作,因为这是一个wordpress网站,我无法在htaccess中呈现短代码。这就是为什么我要在PHP中找到一种方法做这件事的原因。关于什么?