Methods for implementing host header-based multi-domain redirection using ASP and PHP

by jsjsineh3 on 2010-03-30 19:03:22

The following are methods for implementing host header-based multi-domain redirection in ASP/PHP:

### ASP:

1. **Using CASE Statements**: The drawback is that the same domain (both @ and www) requires two loops, resulting in repetitive code.

```asp

```

--------------------------

```asp

0 then

response.redirect "index.asp"

else if instr(Request.ServerVariables("SERVER_NAME"), "bbb.com") > 0 then

response.redirect "x/index.asp"

else if instr(Request.ServerVariables("SERVER_NAME"), "ccc.com") > 0 then

response.redirect "index3.asp"

end if

%>

```

-----------------------

```asp

```

-----------------------------

```asp

```

2. **To resolve the above issues**, you can add a condition to automatically recognize the 'www' prefix:

```asp

```

3. **PHP**:

1. Multiple domains:

```php

```

Or using `switch`:

```php

```

2. Single domain:

```php

```

Or:

```php

";

?>

```

You can also use:

```php

```

**Note**: When using the `Header` function, there must be no output generated on the page. Pay special attention to spaces and ensure it's placed at the very beginning of the page.

### JavaScript:

```javascript

try {

if( self.location == "http://玉米一/" ) {

top.location.href = "http://玉米一/目录";

} else if( self.location == "http://玉米二/" ) {

top.location.href = "http://玉米二/目录";

} else if( self.location == "http://玉米三/" ) {

top.location.href = "http://玉米三/目录";

} else if( self.location == "http://玉米四/" ) {

top.location.href = "http://玉米四/目录";

} else {

document.write ("错误的拜访地址");

}

} catch(e) {}

```

-------------------------------------

```javascript

switch (window.location.hostname) {

case "test" :

// Determine domain as TEST

window.location.pathname = "1"; // Redirect to directory 1

break;

case "127.0.0.1" :

// Determine domain as 127.0.0.1

window.location.pathname = "2"; // Redirect to directory 2

break;

// Add more cases as needed

default :

// If domain not found

window.location.pathname = "3"; // Redirect to directory 3

}

```

#### Location Object Properties:

- `hash`: Sets or retrieves the portion of the URL after the "#" symbol.

- `host`: Sets or retrieves the hostname and port number of the location or URL.

- `hostname`: Sets or retrieves the host name part of the location or URL.

- `href`: Sets or retrieves the entire URL as a string.

- `pathname`: Sets or retrieves the specified filename or path of the object.

- `port`: Sets or retrieves the port number associated with the URL.

- `protocol`: Sets or retrieves the protocol part of the URL.

- `search`: Sets or retrieves the portion of the href attribute that follows the "?" symbol.