开发者

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

开发者 https://www.devze.com 2023-02-08 00:48 出处:网络
I\'m using a GoDaddy hosting account to host multiple domains. How can I show a custom 404 for each of these domains?

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 error开发者_运维百科s 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();
  }
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消