Variable php redirect based on url - php

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...

Related

PHP Redirect to different URLs based on Parameters

I'm currently using this simple redirect (which passes all URL parameters to the next page) to redirect a link:
<?php
function preserve_qs() {
if (empty($_SERVER['QUERY_STRING']) && strpos($_SERVER['REQUEST_URI'], "?") === false) {
return "";
}
return "?" . $_SERVER['QUERY_STRING'];
}
header("Status: 301 Moved Permanently");
header("Location: https://example.com/" . preserve_qs());
?>
One of the issues is that I have with this method is that I need to create a separate file for each redirect.
Is it possible to make this into something that I simply add different URLs inside and based on URL parameter that I call, it sends people to the right URL.
For example, in PHP, we store 3 URLs and we assign them 3 values (parameters):
example1.com = page1
example2.com = page2
example3.com = page3
and the PHP file URL would look like this:
example.com/redirect.php?land=page1?restofparameters
keep in mind that the rest of the parameters need to be sent to the goal page, but not the page1 parameter which calls the URL inside the PHP file.
So the target URL that people will land will would look like this:
example1.com/?restofparamaters
Any help is appreciated. Thank you!
you can try .htaccess method... like this...
In .htaccess file
RewriteEngine On
RewriteRule ^test/(1)? http://triviamaker.com [R=301,L]
then for check this one
localhost/your_project_folder/test/1
Note:- that .htaccess file must be in Root directory of your Project.
& if you have any query or found any problem related to that .htaccess method then you can Search "redirect a specific url to another url with htaccess" in Google. you will Found more details easily about this...
hope this one is Helps to you... Thank You...
I would recommend a link parser.
But here goes an example based on your code for stripping and inheriting GET URL parameters.
<?php
function preserve_qs() {
if (empty($_GET)) return '';
return '?'.http_build_query(array_diff($_GET, ['land']));
}
http_response_code(301);
header('Location: https://example.com/' . preserve_qs());
exit;
?>
I write this in test.php ... just for your Reference
Run This File for check Output...
http://localhost/url_rewrite/test?2
and run also like this http://localhost/url_rewrite/test?land=2
echo $temp = $_GET['land'];
echo $temp = $_SERVER['QUERY_STRING'];
if($temp == "1" || $temp == "land=1"){
header('Location: https://example.com');
}else if($temp == "2" || $temp == "land=2"){
header('Location: https://gmail.com');
}else if($temp == "3" || $temp == "land=3"){
header('Location: https://apple.com');
}else if($temp == "4" || $temp == "land=4"){
header('Location: user/2');
}
I hope this helps to you... Thank you

Redirecting a page Based on URL Value

I am trying to redirecting my register page to https:// based on parse url value.Below is my code
$url = $current_url="//".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$parts = parse_url($url);
parse_str($parts['query'], $query);
if ($query['view']=='register') {
$porthttp = "https://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
header("Location: " . $porthttp);
exit();
}
But it is giving error 'server is redirecting the request for this address in a way that will never complete.'
what's wrong i am doing?
You are redirecting on same page which you accessing so it will not work.
I.e http://localhost/demo/test.php?view=register and you are checking condition like if ($query['view']=='register') {} then you are redirecting on same page using $_SERVER['REQUEST_URI'] so it will go to infinite loop.

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);
}

PHP How to redirect to another page when a particular url come

I want to develop a url Router in which when a particular URL comes then I redirect my program to a particular url. Is it even possible?
Thanks in advance...
You can send a HTTP header to redirect to another page:
header("Location: foo.php");
... or for a full URL:
header("Location: http://www.google.co.uk/");
Note that you should send headers before any other output (i.e. echo).
if you want to test a 'particular' url, you need to construct it first:
$ssl = "";
if ((isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"]=="on") || (isset($_SERVER["SERVER_PORT"]) && $_SERVER["SERVER_PORT"]=="443"))
{ $ssl = "s"; }
$serverport = ($_SERVER["SERVER_PORT"]!="80"?":".$_SERVER["SERVER_PORT"]:"");
$theurl = "http".$ssl."://".$_SERVER["SERVER_NAME"].$serverport.$_SERVER["REQUEST_URI"];
then, you can test it against another url (or array of them):
if ($theurl != $myurl) {
header("Location: index.php");
}
against an array of urls:
if (in_array($theurl,$myurls)) {
header("Location: index.php");
}

Categories