1 domain 2 websites - php

There are 2 different websites in 2 directories ..path/siteA/ and ..path/siteB/ . I need to load one of them on domain example.com depending on their country they are visiting from.
It can't be www.example.com/siteA it must be www.example.com .
Is it posible?
Edit: found the solution.

Configure Your webserver to listen on two different ports, one pourt should serve content from /path/siteA and the second one from /path/siteB.
Next step is to configure Pound depending on the location of the user (IP geolocalisation) and You're on Your way

It is usually done with geo-location.
You use a redirect on example.com/index.php that redirects to example.com/pathA or example.com/pathB depending on their ip
use the header() function to redirect :)
$ip = $_SERVER['REMOTE_ADDR'];
if(...) // check location of IP
{
header("Location: /pathA");
}
else
{
header("Location: /pathB");
}
http://php.net/manual/en/reserved.variables.server.php
http://au.php.net/manual/en/function.header.php
Edit:
Based on comment, this is what you need: mod_Rewrite: Filter specific pages by IP and redirect them

Yes it is possible. First pages you have to ask the language or show the default language.
This is done by two methods:
Each language can be lines or words assigned in separately files with each variables. By using the variable calling we can the get the language parameter.
Each language can be lines or words assigned in database with separately table.

Related

Php Cname redirection

Recently I've been thinking about such a question: how to use php to recognize that a site is referenced with a CNAME record, and if so, redirect it to a specific document?
$_SERVER['SERVER_NAME'] contains the domain name of the site being served, which you can plug into dns_get_record(), filtered to get only CNAME records. So, if the domain name that was used to view the site is a CNAME, the result will not be empty, which you can use as a conditional for your redirect. I can't think of any use case where this would be desired, but something like this:
if (!empty(dns_get_record($_SERVER['SERVER_NAME'], DNS_CNAME))) {
header('Location: <whatever>');
exit;
}

Redirect to exacly same path with diffrent languge

Here is what I'm trying to do:
Whatever page customer open script will redirect him to his preferred language read from $_SERVER["HTTP_ACCEPT_LANGUAGE"];
example:
Customer browser is set in Spanish. If he open page http://example.com/script-name.php?param1=aa&param2=bb
than script will redirect him to http://example.com/es/script-name.php?param1=aa&param2=bb
I have script which provide me with language of the browser $lang=substr(Get_Client_Prefered_Language(),0,2); that is sorted
Than I have script which reads the address
$location = '';
parse_str(html_entity_decode($_SERVER['QUERY_STRING']),$query_string);
if(isset($query_string['language'])) {$query_string['language'] = null;}
$new_query_string = !empty($query_string) ? http_build_query($query_string) : '';
if($new_query_string != ''){$location .= '?'.$new_query_string;}
and that provide me with new address:
$location="https://macrohosting.co.uk/".$lang.$_SERVER["SCRIPT_NAME"].$location;
and than I can use header('location: '.$location);
All works quite well but some of the pages I have already url rewrited.
And not sure how to adjust above script so when customer visit http://example.com/page1.htm than will be redirected for example to http://example.com/es/page1.htm
Without that modification customer is redirected to http://example.com/es/page_example.php?param=aa&param=bb
there is no need for you to redirect user for each request . it's the worth way to handle the problem.
you can get user browser language in front controller (for example index.php file) and then generate links accordingly.
you are trying to hit your web server 2 times for each request?? it's not an option.
if you are using a php framework there should be a better way to handle the problem.
<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
$link_to_about_us_page = "http:://domain.com/$lang/about";
and keep in mind that first parameter after domain in every request is language and according to that language you should fetch content from your storage and show data.(this part should be handled by your router)
and if language is not provided you redirect users to your default language .
for example
http://domain.com/contact
to
http://domain.com/en/contact
thats how drupal handle multilingual. there are multiple things that can affect content language (user browser| url | user ip | ...)
and you can arrange priorities.

Address Bar not changing - PHP

I am using header('Location:') to redirect the user to another webpage. But, instead of overwriting the previous address on the address bar, the new one gets appended to the one already present. For example:
The address bar currently holds:
localhost/v2/admin
and there is header('Location:'.DIRADMIN.'login.php');
where DIRADMIN is a constant defined as
define(DIRADMIN,'localhost/v2/admin/');
Then, after the redirect, the new address bar would be
localhost/v2/admin/localhost/v2/admin/login.php
I thought it was because of the server, I am using. I was using Uniform Server Zero. But then I installed WAMP and the problem continues.
I am still a noob, I have no idea what is causing this and if the problem is because of mod_rewrite, then on both servers the module was active. I already checked some of the problems like
redirect-PHP header(Location:..)
among others. I even did a google search for this but to no end.
How can I solve this problem?
header_remove();
will not work as the first address was typed in manually and not set by header();
Without the http prefix, browser is trying to find the path relative to your current one
Add "http://" as so
header('Location: '.DIRADMIN.'login.php');
If you want URL to be relative to your domain root path, you can just add '/'
header('Location: /'.DIRADMIN.'login.php');
You should always try to use a relative path - That way, if you change your domain, your code still works.
Define
define('DIRADMIN','localhost/v2/admin/);
As
define('DIRADMIN','http://localhost/v2/admin/');

Allowing website access only from a specific network

I'm making a website that appeals to students at my school. I want to only allow access if the user is on the campus wifi or hardwire. Using PHP, how can I restrict access to people I am sure are on the campus internet?
You would need to get a range of IP addresses and put them in a while list. You could then use the $_SERVER['REMOTE_ADDR'] variable to check against the white list for access. Do it at the beginning of the page with something like this:
if(in_array($_SERVER['REMOTE_ADDR'],$white_list)) {
//allow execution code?
} else {
exit;
}
This is usually done in the webserver configuration, which has the advantage of also working for images, but in theory you could put
if ($_SERVER['REMOTE_ADDR'] != '...')
die();
in every of your PHP pages.
At first, you need to get the range of IPs from your school's network admin.
Then from PHP:
$ip=$_SERVER['REMOTE_ADDR'];
if(inRange($ip)) die();
else { ....
Now write inRange($ip) function to return true if the given ip is in the range. you can use explode function to get pieces of the ip to compare.. :)
It's already been mentioned, but the 'right' way to do it is to specify the IP range in the setup of your webserver (IOW, don't do it in PHP)

domains redirecting to specific pages

I have a main domain name associated with a WordPress site, and then I have a couple other domain names connected to that site as well.
I want the other two domains names to point/redirect to specific pages on the site rather than the index page, which is the default.
So when domain1.com is typed into the browser, it goes to maindomain.com/domain1page/ (this is how the permalinks are set up). Is this possible?
Add this to index.php and upload it the root of domain1.com
<?php
header("location:http://maindomain.com/domain1page/");
?>
OR if you do not have a Hosting package for domain1.com go to the Domain Manager and in Nameservers you can enter a URL to redirect your domain to there.
It's possible, but you must add each domain manually. Just redirect them to your page.
Sure - there are lots of different ways to do this. Some registrars let you set up redirects at the domain level. You could also have a website set up for each domain on your server, and then just redirect to the page you'd like from that. You could also use the httpd.ini file to detect the domain and redirect to the appropriate page.
This is a small script that might serve the purpose.
You should place this at the top of header.php in your wordpress theme.
The script will not do anything if a domain is not matched so wordpress will load normal page. Haven't tested, but it should work.
<?php
$host = $_SERVER["HTTP_HOST"];
//Setup Domains Directory Names Here
$domain1 = 'domainname1.com';
$domain1_dir = 'domain1directoryhere';
$domain2 = 'domainname2.com';
$domain2_dir = 'domain2directoryhere';
$domain3 = 'domainname3.com';
$domain3_dir = 'domain3directoryhere';
//Redirects to directory depending domain.
switch (true){
case (preg_match("/$domain1/",$host)):
header("location:/$domain1_dir");
break;
case (preg_match("/$domain2/",$host)):
header("location:/$domain2_dir");
break;
case (preg_match("/$domain3/",$host)):
header("location:/$domain3_dir");
break;
}
?>

Categories