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;
}
?>
Related
I am looking to use some php pages I created as a template across multiple subdomain websites. I want to change minor things across each subdomain (what database is being used and some basic settings but this is not important). I do not want to copy over all files every time I want to create a new subdomain as this will be a pain when making updates. So I want to be able to push updates to my "live" site that acts as a template for all other sites.
This is my current testing layout
I have a "live" site that looks something like this:
-dashboard
-division
-division->lookup
Each of these directories have an index.php that I want included when the appropriate url is visited.
For each subdomain website, I currently have:
-.htaccess file
-errors (includes 404.php)
Within my .htaccess file, I have the 404 page as this 404.php page and that page looks like this:
if((#include "/var/www/live".$_SERVER['REQUEST_URI']."/index.php") === false){
echo "DOES NOT EXIST";}
If the url you are trying to go to exists in "live" it includes it and it basically mirrors it.
This seems to work except for when I try a url that is more than 1 directory deep. For example, if I am trying to go to test.myurl.com/division/lookup I get Google's 404 page (This test.myurl.com page can’t be found).
I am feeling like there has to be an easier way to do this and I am doing this all wrong. Where can I look to do this better? Any tips?
This will check if the file exists and should address the depth issue
<?php
$filename = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];
if (is_file($filename)) {
// file exists as requested
require_once $filename;
exit;
}
http_response_code(404); // may be unnecessary
?>
<!doctype html> ... <!-- 404 page content -->
I am following the link to make wildcard domains. Now m.mysite.com would redirect to mysite.com. But I want it to show a different versions of the site. So that m.mysite.com would be different from mysite.com. My mobile version is put in the htdocs/mobile folder.
So I tried
if (array_shift((explode(".",$_SERVER['HTTP_HOST']))) == 'm'){
include("mobile".$_SERVER['PHP_SELF']);
die();
}
The problem is, I cannot include the Get variables in the include. So if the url is m.mysite.com/read.php?id=1, the part id=1 would be missing.
How should I do it properly?
Note: This question is not about how to redirect different users to different sites according to their devices. But this is about how to set up two sites which shows different versions with a different subdomain.
One way can do this is to download a class that detects what type of device the site is running on (ie. PC, tablet, phone) then load the CSS file appropriately. I have used this class a few times and it seems to work fine.
https://code.google.com/p/php-mobile-detect/
From here you can build up the classes and modify it in whatever way you please.
Solution :
Put two folders in same root directory. First folder is mapped with mysite.com that contain different content and Second one with m.mysite.com with different content.By doing this you can easily access both domains with different content on same web server.
There Is Two Way To Do This Work Simply....
First You Can Detect A Mobile And Redirect Them To A Extra Folder Using This Code
<?php
$agent = $_SERVER['HTTP_USER_AGENT']; // Put browser name into local variable
if (preg_match("/iPhone/", $agent) OR preg_match("/android/", $agent)) { //Detect Mobile Device(You Can Add More Mobile User Agent)
// Send Mobile User To Their Site
header("location: mobile.php"); //ypu can also reirect to a folder or subdomain
}
?>
There Is The Second A Way...You Have To Place This Code In Your Index...
<?php
$agent = $_SERVER['HTTP_USER_AGENT']; // Put browser name into local variable
if (preg_match("/iPhone/", $agent) OR preg_match("/android/", $agent)) { //Detect Mobile Device(You Can Add More Mobile User Agent)
include'mobile.php'; // mobile version
}
else{
include'pc.php'; // dekstop version
}
?>
I have a wordpress blog and don't want others to visit it. So I moved the blog to http://myhost.com/wordpress(sorry for hiding the name) and put an index.php in the root folder of http://myhost.com.
The index.php is simply random redirecting:
<?php
$sites = array("http://google.com", "http://youtube.com", "http://facebook.com", "http://twitter.com")
$len = count($sites);
$num = rand(0, $len - 1);
$str = "Location:".$sites[$num];
header($str);
?>
But sometimes (usually when the network is slow) when I visit http:/myhost.com/wordpress it redirects me to one of these sites I set. This also happens when I click links in the wordpress.
I cannot even identify whether it's browser's action or the server's.
It's probably because Wordpress's default action on 404 is to redirect to your main index.php
If you have access, you can change this in the .htaccess file.
Because you change directory of your Wordpress files but you don't change database datas. You have to change your WOrdpress blog URL from http://myhost.com/ to http://myhost.com/wordpress on wp-admin, (Settings > General section) .
If you can't login to your admin panel, you have to execute these SQL queries :
(don't forget to edit site URL and Wordpress table prefix (wp) !)
UPDATE wp_options SET option_value='http://myhost.com/wordpress' WHERE option_name='siteurl'
UPDATE wp_options SET option_value='http://myhost.com/wordpress' WHERE option_name='home'
Wordpress gets home url from database. Your database pointing home url to http://myhost.com/ . When you click Home Page link it will go to home url (http://myhost.com/)
All my full site pages are under folder public_html, which contains index.php and folder m which contains all the mobile site pages. index.php is like this:
<?php
if (...mobile client) {
header('Location: m/');
} else {
include 'front.html.php';
}
?>
There is also an index.php in m, simply `include 'front.html'.
This script can detect user's client and direct to full site and mobile site automatically.
But now I want a link on the mobile site to switch to full site. If it is like switch to full site, there will be front.html.php in the address bar, and I don't want this. However, if it is like switch to full site, it will detect again and back to mobile site.
How to deal with that?
You can accomplish this by setting a cookie.
setcookie("forceClient", "full", time()+3600);
Then from that php script, redirect to the home page.
In your index.php, check the cookie:
if($_COOKIE["forceClient"] == "full"){
//logic
}
Make a session variable
like
$_SESSION['viewer_type'] = "Mobile";
or
$_SESSION['viewer_type'] = "Regulare";
and then define a new variable call it base_url and save it in the session also
and do this
if($_SESSION['viewer_type'] == 'Mobile'):
$_SESSION['base_url'] = "http://www.m.site.com/";
else:
$_SESSION['base_url'] = "http://www.site.com/";
endif;
the link now will be
Test
You need to set the session variable each time the view changed from mobile to regular or visa versa
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.