2016-08-16 125 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 
    ... 

請讓我知道如果我誤解。
希望它有幫助。乾杯。