Forum Moderators: coopster

Message Too Old, No Replies

php location redirect in a html page

         

berthstar

8:16 am on Feb 1, 2004 (gmt 0)

10+ Year Member


Hi,
I'm having trouble including this php into a html page.
I can't rename the file as a .php file it must be in html.

this is what I have, can anyone help me?

<html>
<head>

<title>redirect</title>
</head>
<body>
<?php
if (strstr($_SERVER['HTTP_ACCEPT_LANGUAGE'], "en-au"))
{
Header("Location: http://www.yahoo.com.au");
exit;
}
?>
test1
</body>
</html>

ams_david

9:11 am on Feb 1, 2004 (gmt 0)

10+ Year Member




All redirect commands should be sent before any other text is sent to the browser.

Otherwise, php will spout errors about "headers already sent".

berthstar

9:53 am on Feb 1, 2004 (gmt 0)

10+ Year Member


yes that is the case when I have a .php file which I can fix by place the code at the biginning of the document
e.g.
<?php
if (strstr($_SERVER['HTTP_ACCEPT_LANGUAGE'], "en-au"))
{
Header("Location: http://www.yahoo.com.au");
exit;
}
?>
<html>
<head>

<title>redirect</title>
</head>
<body>

test1
</body>
</html>

But I want my file to be saved as index.htm

how do I fix this?

mykel79

11:59 am on Feb 1, 2004 (gmt 0)

10+ Year Member



You can simply make the server parse htm files looking for php code.
Add the following line:
AddType application/x-httpd-php .php .php3 .htm

to a file called .htaccess in the directory the .htm file is located. You can name as many file extensions as you like. Whether it will work depends on your host, some don't allow you to modify server behavior like that.

cookie2

3:52 pm on Feb 1, 2004 (gmt 0)

10+ Year Member



I'm curious, could this be written as
AddType application/x-httpd-php .php .php3 index.htm
so that it would parse only the index.htm file and not all .htm files?

ergophobe

4:28 pm on Feb 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It's too bad mod_rewrite does not recognize HTTP_ACCEPT_LANGUAGE. That would solve it all right quick.


I'm curious, could this be written as
AddType application/x-httpd-php .php .php3 index.htm

This adds mime types and associates them with certain file extensions. You would need to either add all .htm files or none. To achieve what you want, you should probably use mod_rewrite on index.htm only and transform it to index.php.

In any case, why does the file absolutely need a .html extension? Is it because the *file* actually needs that extension or because the URI does?

If the former, then just name it .html and use the AddType directive to get it parsed as php.

If the latter, you can name it .html, .php or whatever you want and use mod_rewrite to change the URI. That said, I've said this a bunch of times, but by preference, URIs should not have file extensions. They should be

h**p://domain.com/page/

not

h**p://domain.com/page.html
h**p://domain.com/page.htm
h**p://domain.com/page.php

Tom

berthstar

12:01 pm on Feb 2, 2004 (gmt 0)

10+ Year Member



Thanks Guys,
the .htaccess worked first time.

You can always count on WebmasterWorld :)