how to get real address of urls in php switch statement - php

we have local PHP files A-Z. where redirection switch statement coded. Actually i want to request with id and get REAL address of redirection url. for example, domain1.com full path, domain2.com full path.
please somebody let me know, how to get REAL Domain path of these urls?
<?php
$id = $_GET['id'];
switch ( $id ) {
case 01:
header("Location: http://domain1.com");
break;
case 02:
header("Location: http://domain2.com");
break;
default:
header("Location: http://");
break;
}
?>
How i request by id?
http://localhost/a.php?id=123
and its return Real Address of url like this:
http://www.domain.com/content/title.html // etc

You need something like this?
$path = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

Try
$parts = parse_url('http://domain1.com');
echo $parts['host'];
output will we domain.com and if you need path use $parts['path']

Related

Variable php redirect based on url

i'm creating a index.php file for redirect all website to specific host
I'd like create a little php script that read url and redirect based on specific filter.
for example:
if url = (everything).domain1.com redir to default1.php
if url = (everything).domain2.com redir to default2.php
in all other case that not like first or second redir to default3.php
this is possible with php? i must use $_SERVER['HTTP_HOST'] or can I use other method?
i resolved with:
<?php
$domain = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($domain,'domain-name.com') == true) {
header('location: /index2.php');
exit();
} else {
header('location: /index3.php');
}
?>
but not redirect...

How to Change subfolder in url using php

Please i need help to change requested subfolder URL to other one using php not htaccess file.
From
stackoverflow.com/folder1/ask
To
stackoverflow.com/folder2/ask
is that possible?
update:
I'm working on IP redirect based on geoip mod and bot detected function.
with 3 Magento website (single domain) and two store view with each website.
switch (true)
{ case (bot_detected($bot) === FALSE && $country == "us"):
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : 'us';
break;
case (bot_detected($bot) === FALSE && $country == "uk"):
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : 'uk';
break;
default:
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';
}
Its working fine but if US visitor click on URL from UK store redirect not applied. So what i need enforce the URL to redirected to US and same with any store conditions.
You need to use the pathinfo function. Something like this works:
<?php
$url = 'http://stackoverflow.com/folder1/ask';
$pathinfo = pathinfo($url);
//$pathinfo['dirname'] contains the full prefix of the URL
//here, http://stackoverflow.com/folder1
//So simply replace folder1 by folder2 in that string
$newPrefix = str_replace('folder1', 'folder2', $pathinfo['dirname']);
//Use that new prefix and append the same basename ('ask')
$newUrl = $newPrefix . '/' . $pathinfo['basename'];
echo $newUrl;
Check it out on 3v4l.org!
A more general approach would be using RegEx, if you know the structure of your path.
So for example, you can use the following RegEx:
/stackoverflow\.com\/(.*)\/(.*)/

PHP - Change every url in html depending on domain

Using PHP I would like to be able to change every url in a specific HTML page depending on domain.
This code allows me to change urls one by one but I'm sure there is a better way:
$domain = $_SERVER['SERVER_NAME'];
switch ($domain) {
case "example2.com":
$url1 = "http://".$domain."/secondsubfolder/thispage.html";
break;
case "example3.com":
$url1 = "http://".$domain."/thirdsubfolder/thispage.html";
break;
default:
$url1 = "http://example.com/firstsubfolder/thispage.html";
}
HTML:
first url
Notice that I also need to change the first subfolder depending on domain, thats why I can't use relative urls.
So my aim is to be able change every url in my default HTML code:
hello
bye
should change if my website is accessed through example2.com
<a href="example2.com/secondsubfolder/thispage.html" >hello</a>
bye
Your question isn't very clear to me but I guess you need something like:
<?php
$domain = $_SERVER['SERVER_NAME'];
$scriptPath = $_SERVER['SCRIPT_NAME'];
switch ($domain) {
case "example2.com":
$domain = "example2.com";
$folder = "secondsubfolder";
break;
case "example3.com":
$domain = "example3.com";
$folder = "thirdsubfolder";
break;
default:
$domain = "example.com";
$folder = "thirdsubfolder";
}
$scriptPath = preg_replace('%^/.*?/%', "/$folder/", $scriptPath);
echo "http://{$domain}{$scriptPath}";

Redirect visitor based on referral URL

I want to redirect visitors to one page on my website if they have come from a certain URL and another if they don't.
<?
$referer = $_SERVER['HTTP_REFERER'];
if ( $referer != "http://URL1" ) {
header('Location: page1');
exit;
} else {
header('Location: page2');
}
?>
Whatever referrer I come to the page on it goes to page 1 and never to page 2. I have used this code in a index.php file so its the first page the visitor is directed too.
UPDATE: alright, so from the discussions, it seems that the reason why your code won't work is that you are checking the referer string using the "now-www" url, while the actual referer string has "www" in the url. Please, make sure to use the exact referer string. Otherwise, if you are redirecting based on the hostname of the referer you can use the updated answer below.
<?php
$referer = str_replace("www.", "", parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST));
switch($referer) {
case "johnchow.com":
header("Location: page1");
break;
case "domain2.com":
header("Location: page2");
break;
default:
header("Location: page3");
}
exit;
For starters change this to
if ( $referer != "http://URL1" || $referer != "http://URL2" )
Secondly, page1 and page2 are likely giving the error because they are invalid. Include the path and extension. For example:
header('Location: http://www.yourlocation/page1.php')
Looks like the error has been clarified...

PHP Redirect if language parameter is not in URL

I have a url such as: http://localhost/project and when the user goes there, I want to redirect to http://localhost/project/en. How do I accomplish this?
You can do this programmatically or using something at the web server level (e.g. mod_rewrite with Apache). Since you've mentioned PHP explicitly, I'll provide you the following.
In a folder called 'project' at your document root, create an index.php with the following:
<?php
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://localhost/project/en" );
exit(0);
?>
Here's a link that shows this style and other methods (including mod_rewrite) to handle this:
http://www.phatz.com/301redirect.php
There are many ways. You should be using virtual hosts and rewrite uri to prepare your URL properly, but if you don't want to bother with those and want a method that "just works" exactly for the problem you presented, then try this:
$explode = explode('/',$_SERVER['REQUEST_URI']);
if (count($explode) == 1)){
header('Location: http://localhost/project/en');
}
Edit after comments
Try this, but beware that I haven't tested it, as I don't have my prod. environment here, but it should work. If not, comment :)
$explode = explode('/',$_SERVER['REQUEST_URI']);
$endsWithSlash = FALSE;
end($explode);
if (empty($explode[key($explode)])){
unset($explode[key($explode)]);
$endsWithSlash = TRUE;
}
if (end($explode) != 'en'){
$url = 'http://localhost';
$url .= $_SERVER['REQUEST_URI'];
if (!$endsWithSlash){ $url .= '/'; }
$url .= 'en';
header('Location: '.$url);
}

Categories