I am trying to redirect www.xyz.com/my to www.abc.com, how can I do this in PHP?
Strictly speaking you need to send a HTTP location header to the clients browser.
To do this in PHP, as the other answers have mentioned is to use the header() function.
header("Location: http://www.abc.com");
There is a caveat that you should be aware of. The most common problem people encounter when dealing with HTTP headers is that they must be sent to the browser prior to any other data. If you echo any content to the client and then try to send the location header, it wont work.
For reference, there are many other HTTP headers that you should familiarize your self with.
You may do that in PHP by creating a subdirectory called /my on the http-root of the server where www.xyz.com is hosted and then make an index.php file with <?php header("Location: http://www.abc.com"); ?> inside it.
However, this is not the right way to do it. What you should do it create a .htaccess file right in the root folder of www.xyz.com looking like:
redirect /my http://www.abc.com
You can send a header redirect:
header("Location: http://abc.com");
If the default page for a directory is index.php, just put that code under /my/index.php. Any visits to the /my page will redirect.
header("Location: http://www.abc.com");
wrong technology. You probably want to look into apache htaccess, which is generally what most people use to host php.
Related
I have the following code in my index.php page:
<?php include("/includes/widgets.php") ?>
And in my widgets.php page:
<?php
header("Location: /");
?>
What I want to achieve with this is to redirect it if the user visits it, but allow it for including.
But I get the following error:
The webpage has a redirect loop
How can I fix/prevent the redirect loop, but still redirect the user and not the server.
Place the widgets.php file in a folder not accessible to HTTP clients. I.e on apache webserver, either put it above your document root (into it's parent) or use .htaccess file to block users from it.
e.g.
deny from all
I think I know what you need :)
Change code index file to next
define("IS_INDEX", true);
include("/includes/widgets.php"
Change code for widgets.php to next
if (!defined("IS_INDEX")) {
header("Location: /");
exit();
}
The issue is you are redirecting back to the same page, which then redirect again, and again, and again.
An easy fix would be to wrap the redirect in an if, and only redirect if they aren't already on the index page; but this is just patching what looks like an architectural problem.
Something like:
if (ltrim($_SERVER['REQUEST_URI'], '/') != 'index.php')
header('Location: index.php');
One way is to check if __FILE__, which is the file loaded, regardless of included or not matches up with the file requested which is in $_SERVER['REQUEST_URI'] (or $_SERVER['PHP_SELF']).
I use this on our development site in a page that is usually included to get the output as debugging.
if(basename($_SERVER['PHP_SELF'])===basename(__FILE__)){
//do some debugging
}
Typically you wouldn't use basename, but this is on a non-public facing development site and the file has a pretty unique name so I'm not worried about the file being included with another file with the same name or anything.
One possible way is to add a parameter to the redirection, e.g.
if (!$_REQUEST['redirect'])
header("Location: /ìndex.php?redirect=1");
That way redirection can happen only once.
Another way is to stop redirection if the user already is on the /. I´d suggest to combine both.
I'm sure this has been asked before, but I can't seem to find an answer. I'm working on PHP development, creating a basic login connected with mySQL database. Once the name/password is verified correct, I would like to show a welcome screen. My issue is that during development, I can't use a local directory like:
header("location:script/login_success.php");
From what I've read, header() is looking for an http:// URL. Is there any way to be able to test locally?
Thanks
header() can take any URL, even relative ones. If you are testing something locally, something like header("location: script/login_success.php")
just remember that header() must be called before any content is output or it will do othing
You can create a url for use in a header redirect by doing this.
$url = isset($_SERVER["HTTPS"])?"https://":"http://".$_SERVER["HTTP_HOST"]."/script/login_success.php");
header("Location: $url");
This should resolve correctly on localhost or elsewhere.
Another option would be to set up a constant that represents your base url per environment
define("BASE_URL", "http://localhost/");
and use that to build the url
header ("Location: ".BASE_URL."script/login_success.php");
Header has to be the first function call in page (you can access other functions but no output ones not even space or enter).. above everything(echo and html)... it should work everywhere...
When you echo the response is written partially and header is 200 ok... so... just write header first and then exit();
header('Location: url') ... I don't think url has to be relative... read on php.net
You can pass relative url to header there is no problem.
header("Location: /route/here.php");
is perfectly legit. Although the RFC dictates absolute URLs including the schema must be provided. Some web clients accepts the relative URL. Dagon pointed the RFC Matter.
14.30 Location
The Location response-header field is used to redirect the recipient
to a location other than the Request-URI for completion of the request
or identification of a new resource. For 201 (Created) responses, the
Location is that of the new resource which was created by the request.
For 3xx responses, the location SHOULD indicate the server's preferred
URI for automatic redirection to the resource. The field value
consists of a single absolute URI.
Location = "Location" ":" absoluteURI An example is:
Location: http://www.w3.org/pub/WWW/People.html
Note: The Content-Location header field (section 14.14) differs
from Location in that the Content-Location identifies the original
location of the entity enclosed in the request. It is therefore
possible for a response to contain header fields for both Location
and Content-Location. Also see section 13.10 for cache
requirements of some methods.
a website has used a "301 permanent redirect" to my site is there a way i can set code that detects this and displays a page when my website is accessed through this?
Does anyone have any idea about this?
You can get only a referer. I think you will not be able to get the http status code on server which the client gets during last request.
So my answer is NO, you cannot get the 301 status code on your server.
But you can do a little of needed magic with referer variable.
e.g. in PHP you can read this:
$_SERVER['HTTP_REFERER'];
Not much you can do. If you were doing the 301, you could set the referrer to the querystring. But since you're not, you can only grab what the request has given you.
You can try using PHP's $_SERVER['HTTP_REFERER'] to track the source URL from where your visitor comes from. I think it's a bit dodgy though and might not yield the same result in all browsers. Even PHP's documentation says 'it cannot really be trusted'.
Why do you have to use .htaccess for the redirect? You could do something like this:
Site A's index.php:
header("Location: http://siteb.com/?ref=".urlencode('http://sitea.com');
Site B's index.php:
if(isset($_GET['ref']))
{
if($_GET['ref']=='http://sitea.com')
{
// Do something
}
}
Edit:
If you can't edit Site A's code or server settings, try using:
if($_SERVER['HTTP_REFERER']=='http://sitea.com')
{
// Do something
}
for example if in my index.php i have something like:
<?php
header('Location: /mypublicsite/index.php');
?>
what do the crawlers and/or robots get? just a blank page? or they actually arrive to /mypublicsite/index.php?
Initially, they get a blank page, with a header saying they should load a different page instead. The client has to load the new page itself.
Robots do understand the Location directive, and will load the new page instead.
You have to understand though you should stop the execution of your php script yourself, because the Location header can be ignored.
So something like this:
<?php
header('Location: otherpage.php');
echo $secret;
?>
is unsafe, because $secret will be sent to the client.
They arrive at the target of the redirection.
The header information of the document will be read by the crawler. The robot will go to the url because the location entry tells everybody to redirect to the given URL.
See http://www.theinternetdigest.net/archive/301-redirects-seo.html and http://jesperastrom.com/seo-301/different-variations-of-redirects-301-302-303-304-etc/
Possible Duplicate: How to make a redirect in PHP?
Hi!
How do i forward a page on the best way? Should I use the header-funct. or should i use HTML (meta-tags) to refresh? I hope some experts could give me some advice at this point. Thanks!
Btw, the forwarding is made inside an if-statement if that could be to some problem?
If you want to redirect the user to an URL, you can use the header function to send a Location HTTP header :
header('Location: http://www.example.com/new-url.php');
die;
(In theory, you should use an absolute URL that includes the domain name -- but most browsers accept a non-absolute URL)
You can use this wherever you want in your script, even inside a if-block, of course.
The only thing is, as you are setting an HTTP-header : you must not have sent any kind of output before (not even a white space at the end of an included file).
You can use JavaScript.
echo "<script>location.replace(\"$url\");</script>";
Take a look at https://www.w3schools.com/howto/howto_js_redirect_webpage.asp .