I am making a redirect page on Wordpress. The PHP will return back to the website homepage. But I can't get back the home page url.
My php file path xampp\htdocs\wordpress\return.php.
And here is my code:
$url = "$_SERVER[HTTP_HOST]";
header('Refresh: 3;url=' . $url);
echo get_option('home');
echo $url;
The $url is localhost8080/wordpess/return.php.
I want to go url : local:8080/wordpress from url : localhost8080/wordpess/return.php.
How can I get back the url local:8080/wordpress?
Thx
Wordpress has a built-in function for that: wp_redirect(see 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
From what I understand, you're trying to redirect your page from localhost:8080/wordpess/return.php to localhost:8080/wordpess/ using -
$url = "$_SERVER[HTTP_HOST]";
header('Refresh: 3;url=' . $url);
What you need to do is change your $url variable to the location where you want to redirect, which is -
$url = "http://localhost:8080/wordpess/";
header('Refresh: 3; url =' . $url);
Hope that's what you were looking for.
EDIT -
If you don't want to hard code the URL, you can try the following -
$url = "/wordpess";
header("Refresh: 3; url = http://" . $_SERVER['HTTP_HOST'] . $url);
From my understanding of your question, you want to go back one level from the current page. This is it?
If so, you can accomplish that by doing some string manipulation as follows:
<?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
...
Please let me know if I misunderstood.
Hope it helps. Cheers.
Related
We are directing all incoming traffic in a directory to a specific page because not all content is ready. Traffic is being redirected to a specific page. It is what it is, and the URL structure aint pretty. I need to get the incoming URL - before the redirect, modify one part of it and display it on the page.
I can get the incoming URL like this
$incoming_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
which would ouput this: http://example.com/dir1/dir2/dir3/dir4-lang/Page.php
I need to take that url, and ONLY CHANGE the /dir4-lang/ to /dir4-newLang/ so the URL I display on the page would be http://example.com/dir1/dir2/dir3/dir4-newLang/Page.php
I think I'm on the right track but need some help with:
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$my_var = 'en-en';
$temp = explode( '/' , $url );
$temp[6] = $my_var;
$url = implode( '/' , $temp );
It is close but the incoming URL is http://example.com/dir1/dir2/dir3/dir4-lang/Foo.php and I am outputting
Try somthing like this:
$my_var = 'dir4-newLang';
$temp = explode( '/' , $_SERVER['REQUEST_URI'] );
$key = array_search('dir4-lang', $temp);
$temp[$key] = $my_var;
$last = count($temp) - 1;
$temp[$last] = strstr($temp[$last], '?');
echo $url = "http://$_SERVER[HTTP_HOST]" . implode( '/' , $temp );
So http://example.com/dir1/dir2/dir3/dir4-lang/Page.php?stuff gonna output http://example.com/dir1/dir2/dir3/dir4-newLang/?stuff
Hello I'm currently working with php to generate a menu with a own build CMS system.
I'm making a dynamic link with : $url = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."/";
Than I'm adding . $row_menu['page_link'] from the database. At first it works perfect:
as example =
$row_menu['page_link'] = page2;
$url . $row_menu['page_link'];
it will return as example : http://example.com/page2
But when I click again, it adds page2 again like : http://example.com/page2/page2
How do i prevent this?
Thanks in advance!
Because at first time your $_SERVER['REQUEST_URI'] will be like http://example.com but when the user click on the link then the value of $_SERVER['REQUEST_URI'] would become http://example.com/page2.That's why it is appending two times.
Instead you can use HTTP_REFERER like
$url = $_SERVER['HTTP_REFERER'].$row_menu['page_link'];
Considering that your $_SERVER['HTTP_REFERER'] will results http://example.com.Also you can try like
$protocol = 'http';
$url = $protocol .'//'. $_SERVER['HTTP_HOST'] .'/'. $row_menu['page_link'];
REQUEST_URI will give you whatever comes after example.com, so leave that out all together.
$url = $_SERVER['HTTP_HOST'] . "/" . $row_menu['page_link'];
You can find a full list of the $_SERVER references here.
Try this:
$requested_uri = $_SERVER['REQUESTED_URI'];
$host = $_SERVER['HTTP_HOST'];
$uri_segments = explode('/',$requested_uri);
$row_menu['page_link'] = 'page2';
if($row_menu['page_link'] == $uri_segments[sizeof($uri_segments)-1]) {
array_pop($uri_segments);
}
$uri = implode('/',$uri_segments);
$url = 'http://'.$host.'/'.$uri.'/'.$row_menu['page_link'];
echo $url;
I am struck in getting the URI in my wordpress application and lack of PHP knowledge is making my progress slow.
I have this URL
http://abc.com/my-blog/abc/cde
i need to create a URL something like
http://abc.com/my-blog/added-value/abc/cde
where http://abc.com/my-blog is the URL of my wordpress blog which i can easily get using following method
home_url()
i can use PHP $_SERVER["REQUEST_URI"] to get request URI which will come up as
/my-blog/abc/cde
and than i have no direct way to add value as per my requirement
is there any way to achieve this easily in PHP or Wordpress where i can get following information
Home URL
Rest part of the URL
so that in end i can do following
Home-URL+ custom-value+Rest part of the URL
My point of Confusion
On my local set up $_SERVER["REQUEST_URI"] is giving me /my-blog/abc/cde, where /my-blog is installation directory of wordpress and i can easily skip first level.
On production server its not same as /my-blog will not be part of the URL.
Very briefly:
<?php
$url = "http://abc.com/my-blog/abc/cde";
$parts = parse_url($url);
$path = explode("/", $parts["path"]);
array_splice($path, 2, 0, array("added-part")); //This line does the magic!
echo $parts["scheme"] . "://" . $parts["host"] . implode("/",$path);
OK, so if $addition is the bit you want in the middle and $uri is what you obtain from $_SERVER["REQUEST_URI"] then this..
$addition = "MIDDLEBIT/";
$uri = "/my-blog/abc/cde";
$parts = explode("/",$uri);
$homeurl = $parts[1]."/";
for($i=2;$i<count($parts);$i++){
$resturl .= $parts[$i]."/";
}
echo $homeurl . $addition . $resturl;
Should print:
my-blog/MIDDLEBIT/abc/cde/
You might want to use explode or some other sting function. Some examples below:
$urlBits = explode($_SERVER["REQUEST_URI"]);
//blog address
$blogAddress = $urlBits[0];
//abc
$secondPartOfUri = $urlBits[1];
//cde
$thirdPartOfUri = $urlBits[2];
//all of uri except your blog address
$uri = str_replace("/my-blog/", "", $_SERVER["REQUEST_URI"]);
This is a reliable way to get current url in PHP .
public static function getCurrentUrl($withQuery = true)
{
$protocol = stripos($_SERVER['SERVER_PROTOCOL'], 'https') === false ? 'http' : 'https';
$uri = $protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
return $withQuery ? $uri : str_replace('?' . $_SERVER['QUERY_STRING'], '', $uri);
}
You can store the home url in a variable, using wordpress, using get_home_url()
$home_url = get_home_url();
$custom_value = '/SOME_VALUE';
$uri = $_SERVER['REQUEST_URI'];
$new_url = $home_url . $custom_value . $uri;
I'm trying to change a value in a string that's holding my current URL. I'm trying to get something like
http://myurl.com/test/begin.php?req=&srclang=english&destlang=english&service=MyMemory
to look like
http://myurl.com/test/end.php?req=&srclang=english&destlang=english&service=MyMemory
replacing begin.php for end.php.
I need the end.php to be stored in a variable so it can change, but begin.php can be a static string.
I tried this, but it didn't work:
$endURL = 'end.php';
$beginURL = 'begin.php';
$newURL = str_ireplace($beginURL,$endURL,$url);
EDIT:
Also, if I wanted to replace
http://myurl.com/begin.php?req=&srclang=english&destlang=english&service=MyMemory
with
http://newsite.com/end.php?req=&srclang=english&destlang=english&service=MyMemory
then how would I go about doing that?
Assuming that you want to replace the script filename of the url, you can use something like this :
<?php
$endURL = 'end.php';
$url ="http://myurl.com/test/begin.php?req=&srclang=english&destlang=english&service=MyMemory";
$pattern = '/(.+)\/([^?\/]+)\?(.+)/';
$replacement = '${1}/'.$endURL.'?${3}';
$newURL = preg_replace($pattern , $replacement, $url);
echo "url : $url <br>";
echo "newURL : $newURL <br>";
?>
How do you want them to get to end.php from beigin.php? Seems like you can just to a FORM submit to end.php and pass in the variables via POST or GET variables.
The only way to change what page (end.php, begin.php) a user is on is to link them to another page from that page, this requires a page refresh.
I recently made a PHP-file for this, it ended up looking like this:
$vars = $_SERVER["QUERY_STRING"];
$filename = $_SERVER["PHP_SELF"];
$filename = substr($filename, 4);
// for me substr removed 'abc/' in the beginning of the string, you can of course adjust this variable, this is the "end.php"-variable for you.
if (strlen($vars) > 0) $vars = '?' . $vars;
$resultURL = "http://somewhere.com" . $filename . $vars;
I am trying to figure out how to add s after HTTP once a user checks a box in the html form.
I have in my PHP,
$url = 'http://google.com';
if(!isset($_POST['https'])) {
//something here
}
So basically, when the user checks a box with the name="https" i want to add s to $url's http making it https://google.com.
I have little knowledge on PHP and if someone can explain to me how to go about doing this, this would be really helpful! thanks.
$url = preg_replace("/^http:/i", "https:", $url);
$url = str_replace( 'http://', 'https://', $url );
One way:
$url = '%s//google.com';
$protocol = 'http:';
if(!isset($_POST['https'])) {
$protocol = 'https:';
}
$url = sprintf($url, $protocol);
I don't know on how many pages you want this to happen onward the user checks the box, but one answer is JavaScript and the base tag.
With the base tag, you can force a different origin, what your relative URL-s will be resolved against.
I you are using it ina form, and the user ticks the checkbox them sumbits the form, all other pages will be viewed from the https site, so you can use relative URL-s everywhere, just insert a different base tag when the user wants to change the site form or to http(s).
A solution that doesn't replace urls which contain other urls, for instance http://foo.com/redirect-to/http://newfoo.com
$desiredScheme = "http"; // convert to this scheme;
$parsedRedirectUri = parse_url($myCurrentUrl);
if($parsedRedirectUri['scheme'] !== $desiredScheme) {
$myCurrentUrl= substr_replace($myCurrentUrl, $desiredScheme, 0, strlen( $parsedRedirectUri['scheme'] ));
}
When you consider case-insensitive, then use str_ireplace function.
Example:
$url = str_ireplace( 'http://', 'https://', $url );
Alternatively, incase you ant to replace URL scheme in CDN files.
Example:
$url = str_ireplace( 'http:', 'https:', $url );
This... (with 'https:')
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
Becomes... ('https:' has been replaced)
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
$count = 1;
$url = str_replace("http://", "https://", $url, $count);
Note : Passing 1 directly will throw fatal error (Fatal error: Only variables can be passed by reference) so you have to pass it last parameter by reference.