Display message if URL redirect - php

I have a primary domain (example.com) with WordPress installed. A simple domain re-direct from example.com to example-usa.com has been put in place from the hosting control panel.
My client would like a simple welcome message displaying to anyone that has come from example-usa.com.
To achieve this, I opted for the following PHP:
<?php
$host = $_SERVER['HTTP_HOST'];
if ($host == "example-usa.com" or $host == "www.example-usa.com") {
echo '<header><h1>USA Message</h1>';
}
else {
echo '<header><h1>Normal welcome message</h1>';
}
?>
Unfortunately, when the user arrives to the site, the usa domain disappears from the browser URL bar and is replaced with the primary domain. This means my PHP isn't kicking in as it should.
Is there another way I can achieve this? I'd rather not use a plugin or location service.
It's important to note that there are two re-direct options from the clients domain registrar (123-reg). I've currently picked the 301 option as this is a permanent re-direct. I can however, pick a 302 direction which will introduce an iFrame.

I don't think you'll be able to do this with a redirect in your hosting. Your best bet would be to handle the redirect in your code and set a cookie before you do the redirect. Then on the destination side you can check for that cookie before you show the message.

HTTP_HOST contains the current domain, you can use the HTTP_REFERER to check the previous domain. This one will match with your 'usa' domain after the redirect.

Try this:
<?php
$host = parse_url(get_home_url(), PHP_URL_HOST); // will return 'example-usa.com'
if ($host == "example-usa.com" or $host == "www.example-usa.com") {
echo '<header><h1>USA Message</h1>';
}
else {
echo '<header><h1>Normal welcome message</h1>';
}
exit;
?>

Try this. Since you are redirecting user you will need to look at referrer URL^
if (strpos($_SERVER["HTTP_REFERER"], 'example-usa.com') !== false) {
echo 'Welcome to example.com';
}

Related

PHP Http Referer redirects me even if am visiting from Referer

<?php
ob_start();
require_once '../autoit/includes/db.php';
include 'ss_setting.php';
if(!isset($_SERVER['HTTP_REFERER'])){
// redirect them to your desired location
header('location:../autoit/index.php');
}
?>
this code only works when I add this
Visit code to another php address.
I want to add shorten link like ouo.io or adfly to only get access my
link it always redirect me to index.php even if am i visiting from
another site
If adfly domain is adf.ly we can use parseurl() to check exact domain.
$ref = $_SERVER['HTTP_REFERER'];
$refData = parse_url($ref);
if($refData['host'] !== 'adf.ly') {
header('location:../autoit/index.php');
//or what you want
}

How to create multi-regional website using wordpress

I'm trying to make a multi-regional website using WordPress. i'm working on url http://localhost/siteone I want when someone come from usa redirect to the localhost/siteone/usa/
I edit the function.php file of wordpress theme with code below, but getting error the url become localhost/siteone/usa/usa/usa/usa
// Detacting user location using ipstack
$json = file_get_contents('http://api.ipstack.com/check?access_key=ACCESSKEY&language=en');
// Converts it into a PHP object
$data = json_decode($json);
$loc = $data->country_code;
//NA = northamerica
//End of Decating user location
if($loc=="NA"){ header("Location: usa/"); }
This works very well sidealone but when I add in function.php in theme file not working and give me a error. what should I do. Should i use some session or anything eles.
It looks like it's trying to redirect multiple times, since it'll check the person's location, then redirect, then after redirecting it'll check again, and redirect again, etc.
What you could do is check whether "/usa" is already in the page URL, and if not, redirect, something like this could work:
if ($loc === "NA" && strpos($_SERVER['REQUEST_URI'],'usa/') === false) {
header("Location: usa/");
}
$_SERVER['REQUEST_URI'] is the URL of the current page, not including the domain.
For languages and create diferents regionals website, I'd recommend you this plugins (pro):
https://polylang.pro/ (just one paid)
enter link description here (anual fee)
Custom code based on language:
<?php
if(pll_current_language() == 'en') {
echo 'Only in english';
} else if(pll_current_language() == 'fr') {
echo 'Seulment en francais';
}
?>
If you prefer control language throught web server, use better: $_SERVER['HTTP_ACCEPT_LANGUAGE']
Link: https://www.php.net/manual/en/locale.acceptfromhttp.php
Regards!

Redirection of specific IP address to specific page in PHP

I have used below code to redirect specific IP address to specific page/website. It is working fine in Google Chrome but in Firefox and Internet Explorer, I am getting Error:
"The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete."
Code:
<?php
$visitor = $_SERVER['REMOTE_ADDR'];
if(preg_match("/192.168.1.187/",$visitor)) {
header('Location: http://yahoo.com');
}
else {
header('Location: http://www.google.com');
};
?>
Seems like there is an endless redirect to the URL , Clear your cookies once and try adding HTTPS before the URLs and give a shot.
Also add an exit or die after your header function. That will do.
Like..
<?php
$visitor = $_SERVER['REMOTE_ADDR'];
if(preg_match("/192.168.1.187/",$visitor)) {
header('Location: https://yahoo.com');
exit; //<-- Here
}
else {
header('Location: https://www.google.com');
exit; //<-- Here
};
?>
Tested the script in Nightly 29 (Firefox). I didnt get an error at all. But you should add exit behind each header so the script interrupts. And maybe you should add www in front of the yahoo homepage.

How can I show custom 404 pages for mulitple domains under one hosting account?

I'm using a GoDaddy hosting account to host multiple domains. How can I show a custom 404 for each of these domains?
All of the non-primary domains are in folders in the root of the primary domain.
I tried setting the custom domain page in the hosting account options for the primary domain to a 404.php that then redirected errors on the non-primary domains to their own respective 404.html.
It looked like this:
<?php
$ref = $_SERVER['SERVER_NAME'];
if(strpos($ref, "example2.com"))
{
header( 'Location: http://example2.com/errorpage.html' ) ;
}
elseif(strpos($ref, "example3.com"))
{
header( 'Location: http://example3.com/errorpage.html' ) ;
}
else
{
header( 'Location: http://www.example.com/error/' ) ;
}
?>
No dice. Has anyone had success with this technique?
After spending an hour or so researching this I decided to keep it simple.
First upload a customized /error.html page.
Then simply upload an .htaccess file for each hosted domain that says:
ErrorDocument 404 /404.html
It should take less than 2 minutes.
Create a separate .htaccess file in each folder with it's own set of rules:
ErrorDocument 404 http://otherdomain.com/404.html
I was actually running into the same exact problem. I am currently hosting 5 domains in my deluxe hosting account.
This is how I easily solved the issue...
STEP1:
Set your Godaddy 404 domain page to a custom url (http://www.yourwebsitename/notfound.php). Create this page (notfound.php) in your root directory (Unfortunately Godaddy's custom 404 redirect only works in the root).
STEP 2.
On the landing page of the domain the customer visits, create a COOKIE that includes the full path to the website.
<?php
$redirectionname = $_SERVER["SERVER_NAME"]; // GETS URL PATH OF THE CURRENT DOMAIN BEING VIEWED (WWW.YOURSITENAME.COM).
setcookie("RedirectSite",'',time()-3200); // KILLS THE COOKIE IF IT ALREADY EXISTS AND STARTS FRESH.
setcookie("RedirectSite",$redirectionname); // SETS THE COOKIE TO DOMAIN URL "WWW.YOURSITENAME.COM".
// THIS WILL CREATE THE COOKIE NAME "RedirectSite" WITH THE VALUE OF "WWW.YOURSITENAME.COM".
?>
STEP 3:
Now that we have the customer's browser remembering a cookie with the website's name, we have to create a way for the 404 landing page to recognize this...
<?php
// REDIRECT CUSTOMER TO CUSTOMER 404 WITHIN SPECIFIC SITE (SUBDOMAIN).
if(isset($_COOKIE['RedirectSite'])) //CHECKS TO SEE IF THE URL TO THE SITE IS SET AS A COOKIE
{
$RedirectSite = "http://"; // THE SITE COOKIE DOES NOT HAVE THIS PREFIX, SO YOU MUST APPEND IT FOR REDIRECT TO WORK.
$RedirectSite .= $_COOKIE['RedirectSite']; // THIS IS YOUR SITE STORED IN A COOKIE (ie: www.yoursitename.com)
$RedirectSite .= "/oops.php?error=1&type=404"; // THIS IS THE PATH TO YOUR CUSTOMER 404 PG. NOTE: MUST INCLUDE "/" BEFORE PG.
echo "redirecting..."; // IF THE USER'S BROWSER IS SLOW, IT LET'S THEM KNOW THEY ARE BEING REDIRECTED. THIS SHOULD NOT SHOW NORMALLY.
header("Location: ".$RedirectSite.""); // REDIRECT BROWSER TO CUSTOM 404 URL PG.
exit(); // KILLS ALL CODING AFTER THIS SCRIPT
}else{
echo "Please press BACK"; // RESPONSE BACK IF THE COOKIE IS NOT SET. THE USER MUST MANUALLY GO BACK.
}
?>
STEP 4:
Create your custom 404 pg within the directory you want it to show (oops.php). Use PHP so you can attach variables to the url for $_GET purposes.
<?php
// "http://yoursitename.com/oops.php?error=1&type=404"
$response= $_GET['error']; // GETS THE VARIABLE "error" FROM THE URL.
if($response == '1') // CHECKS TO SEE IF SERVER GAVE USER AN ERROR. "1" MEANS YES.
{
echo "There was an error opening this page. See below..."; // REDIRECTED BY AN ERROR
}else{
echo "Everything is great! No error to report. Have a great day! :)"; // THE USER VISITED THE 404 PAGE WITHOUT BEING REDIRECTED BY AN ERROR.
exit();
die();
}
?>
<h1>404!</h1>
<h2>Page does not exist...</h2>
STEP 5:
YOU ARE DONE! Enjoy your new custom 404 page for each domain in your hosting plan!
STEP 6:
Create beautiful designs.
STEP 7:
Ready?... Set... CODE!
I ran into the same problem. I have around 20 domains hosted on GoDaddy deluxe account I am using the following script and its working fine for me:
<?php
$hostName = $_SERVER['SERVER_NAME'];
$hostSlices = explode(".",$hostName);
if ($hostSlices[0]=="www") {
array_shift($hostSlices);
}
$domain = join(".",$hostSlices);
if ($domain == "domain1.com") {
header('Location: http://domain1.com/404');
exit();
} elseif($domain == "domain2.com") {
header('Location: http://domain2.com/404/');
exit();
} elseif($domain == "domain3.com") {
header('Location: http://domain3.com/404/');
exit();
} else {
exit();
}
?>

How do I redirect links?

I have created one application for my portfolio.
Let's say the URL is http://fb.domain.com/about/ and my FB app is http://apps.facebook.com/myap/about/.
A user can access http://fb.domain.com/about/ directly from my site.
If the user types http://fb.domain.com/about/ or clicks http://fb.domain.com/about/, how do I redirect them to http://apps.facebook.com/myapp/about/
Here's an example from http://www.oodle.com.
If you go to this link, you will be redirected to here.
How is that done? Let me know. I currently use php for my site.
You can use ModRewrite in your .htaccess to redirect based on the rules set, which is probably the cleanest solution without involving hardcoding redirects in your code.
Something like this should work.
// Define current URL
$host = $_SERVER['HTTP_HOST'];
$self = $_SERVER['PHP_SELF'];
$query = !empty($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : null;
$url = !empty($query) ? "http://$host$self?$query" : "http://$host$self";
// If current URL is not Facebook's, redirect to Facebook
if($url == 'http://fb.domain.com/about/'){
header("Location: http://apps.facebook.com/myapp/about/");
exit();
}
Assuming all you need is the simple redirect, no page loading, then this should do it...
header("Location: http://apps.facebook.com/myapp/about/");
exit();

Categories