PHP header location redirect issue - php

When I try to redirect page using the header('Location: ...') function it is appending the redirect URL to current pages URL instead.
For Example, my current page is http://www.example1.com.
I have created a simple file called test.php with the code below in it:
header('Location: http://www.example2.com');
However it is currently redirecting the page to http://www.example1.com/http://www.example2.com instead of http://www.example2.com.
Any suggestion what should be the issue?
This issue has arisen after moving the site to a new server. I am running the site by doing host entry.

Related

How can I restrict URL shortcuts on my website?

What I want is rather simple but I cannot figure it out. I want that if someone tries to access any of the webpages directly on my website then he/she must be redirected to the home page of the website.
For Example : Someone tries to access www.domain-name.com/aboutus he gets re directed to www.domain-name.com/
you can check referrer to block any direct access to your website.
include this code in every page of your website.
<?php
$ref = $_SERVER['HTTP_REFERER'];
$url = "http://www.domain-name.com";
// $url is the main url of your website
if($ref==$url){}else{
header("location:$url");
}
?>
But it is completely deepened on browser to send the referrer header,
so it may not work sometimes.
You can use 2 way do it:
In your code, directed www.domain-name.com/aboutus to www.domain-name.com
Config webserver (apache, nginx)

wordpress site: Apache redirect URI path

We have a wordpress site http://10.10.10.10/sitename/.
When the user enters a "string" in the search bar and hits enter, the request is sent to http://10.10.10.10/?s= and is thrown the default Apache index.html.
Expected behavior: The user should be sent to http://10.10.10.10/sitename/?s= instead of http://10.10.10.10/?s=
What are the possible ways to implement a redirect from /?s= to /sitename/?s=
Thank you,
You have to use the wp_redirect() function of the wordpress
$url ="http://10.10.10.10/sitename/?s=";
wp_redirect($url);
here is the wordpress documentation page of this function

redirect a url to an HTML page

I am trying to route an external webpage to a local file on my machine.
For example:
https://example.org/something/page.php
should redirect to:
c:\local.html
I don't want to redirect all traffic from that domain, only this particular page.
I do not have access to the website so I can't change it.
It's a PHP script. This means you can use PHP's header() function to do what you need.
You'd use it like this:
<?php
header('Location: file://path/to/file.html');
?>
If you want to redirect to a different page on the same server simply use
<?php
header('Location: /path/to/file.php');
?>
This would cause all traffic that lands on that page to be redirected to that local path.
For the best performance you'd want to put this at the top of your script so that the page does not execute more PHP code than needed.
Also do note that any local files will not be visible for other users visiting your page on the interweb.
This means that whenever someone lands on page.php he or she will be redirected to the same path on their local PC so if that is not there they will get a 404 of some kind.
If you want it to happen immediately as you visit the page, add this inside your <head>-tag.
<meta http-equiv="refresh" content="0; url=file:///c:/local.html" />
Same can be achieved with Javascript, if you add this to your <script>-tag inside your <head>-tag.
window.location = "file:///c:/local.html"
And since I see you're using PHP, this might be ideal to you as well:
header('Location: file:///c:/local.html');
Using either one works, but I would prefer the PHP option. Simply add it inside your <php>-tag (at the top would be prefered).

Visting direct links after a redirection cookie exists

I have this code for my wordpress site
<?php
if(!isset($_COOKIE['iwashere']) || ($_COOKIE['iwashere'] != "yes")){
setcookie("iwashere", "yes", time()+20000,'howtobuygoldoffshore.com');
header("Location: http://www.howtobuygoldoffshore.com/sitemap");
exit;
}
it works perfectly well and redirects new users based on cookies .
Now if someone wants to visit a link directly ie:http://howtobuygoldoffshore.com/process-payment they cant open it since it redirects them to a landing page due to to the cookie redirection
I wish to use it both the ways. A new user should also be able to visit direct links properly without seeing the landing page if he is going directly to the links such as http://howtobuygoldoffshore.com/process-payment
Can this be possible?
Have a look at $_SERVER['REQUEST_URI'].
You could check if index.php (or any other landings page) is given or something similar, and do or don't redirect.

PHP: Best way to redirect a nonexistent url?

I have a site I'm working on with a /landing-page/ folder, and I'll be making a number of landing pages. I'd like to be able to put urls that have /lp/ instead of /landing-page/ in advertisements, and have any url with /lp/ in it replaced with /landing-page/.
Thus:
www.site.com/lp/ipad-inspections
would automatically redirect to:
www.site.com/landing-page/ipad-inspections
I would like to do this without having a /lp/ folder and a second page corresponding to each landing page. My thought is to have the 404 page check the url and redirect, but I can't seem to get the following to work:
<?php /* Automatic redirect for landing pages */
$current_loc = $_SERVER['REQUEST_URI'];
$short_lp = '/lp/';
$long_lp = '/landing-page/';
if (strpos($current_loc, $short_lp)) {
$current_loc = str_replace($short_lp, $long_lp, $current_loc, 1);
header("location: ".$current_loc);
}
What am I doing wrong here, that my page is coming up blank? I have narrowed it down to the first line in my if statement, which is crashing my page.
If there a better way to do this with apache? Some line I can put in htaccess, maybe?
RedirectPermanent /lp /landing-page
in your .htaccess should do the trick. Best to do this sort of unconditional redirect BEFORE it reaches the PHP stage. It's essentially a "free" operation in Apache, and saves your server the whole parse/compile/execute PHP stages.
Keep it as you've done it but include the 301 permanently moved header.
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: '.$location);
Also try referencing your URL absolutely, so as to include http://www.domain.com/
Update
If you want to redirect with htaccess try
redirect 301 /lp/landing-page/ http://www.domain.com/new.html

Categories