2016-08-16 198 views
0

我在Wordpress上制作一个重定向页面。 PHP将返回到网站主页。但我无法取回主页的网址。如何获取主页网址?

我的php文件路径xampp\htdocs\wordpress\return.php

这里是我的代码:

 $url = "$_SERVER[HTTP_HOST]"; 
     header('Refresh: 3;url=' . $url); 
     echo get_option('home'); 
     echo $url; 

$urllocalhost8080/wordpess/return.php。 我想从url : localhost8080/wordpess/return.phpurl : local:8080/wordpress

如何取回网址local:8080/wordpress

THX

+0

通过'localhost8080/wordpess/return.php',你真的指'localhost:8080/wordpress/return.php'吗? – RRikesh

回答

1

的WordPress有一个内置功能:wp_redirectsee doc

require_once(dirname(__FILE__) . '/wp-load.php'); // first you need to load WordPress libraries if you are in an external file. 

wp_redirect(home_url()); 

exit; // don't forget to exit after a redirection 
0

据我所知,你正在使用您的页面重定向从localhost:8080/wordpess/return.phplocalhost:8080/wordpess/ -

$url = "$_SERVER[HTTP_HOST]"; 
header('Refresh: 3;url=' . $url); 

你需要做的是改变你的$url变量的位置,你有什么想要重定向,这是 -

$url = "http://localhost:8080/wordpess/"; 
header('Refresh: 3; url =' . $url); 

希望这就是你要找的。

编辑 -

如果你不想硬编码的网址,你可以尝试以下 -

$url = "/wordpess"; 
header("Refresh: 3; url = http://" . $_SERVER['HTTP_HOST'] . $url); 
+0

是的,我想将页面重定向到'localhost:8080/wordpress'。但我不想硬编码路径。 – Capslock10

+0

@ Capslock10我已经更新了答案。希望这对你有用。 –

-1

从我对你的问题的理解中,你想回到当前的一个级别p年龄。就是这个?

如果是这样,你可以完成,通过做一些字符串操作如下:

<?php 
    // Given that your current url is in the '$url' var 
    $url = 'localhost8080/wordpess/return.php'; 

    // Find the position of the last forward slash 
    $pos = strrpos($url, '/'); 

    // Get a substring of $url starting at position 0 to $pos 
    // (if you want to include the slash, add 1 to the position) 
    $new_url = substr($url, 0, $pos + 1); 

    // Then you can have the redirection code using the $new_url variable 
    ... 

请让我知道如果我误解。
希望它有帮助。干杯。