Redirect

E

Edwin van der Vaart

chlori said:
Hi

How can I redirect visitors from www.domain.com to www.domain.com/folder?

So, if I type www.domain.com I see www.domain.com/folder

I tried with .htaccess
Redirect permanent http://www.domain.com/ http://www.domain.com/folder/
(on one line) but it didnt't work.

Remove the permanent, than it should be working fine.
For more info.
http://wsabstract.com/howto/htaccess7.shtml
Did I do something wrong? Or is there a way with PHP?

You can redirect with php like:
<?php header(Refresh: 5;url=http://www.xxx.com/"); ?>
 
E

Eric B. Bednarz

Edwin van der Vaart said:
chlori wrote:

Remove the permanent,

Why? Maybe it *is* permanent, but it would probably better to start
with the reason for the objective, not the technical details.

Preferably RTFM.

<http://httpd.apache.org/docs/mod/mod_alias.html#redirect>

The argument for the old resource is an URL-*path* (and the status is
optional).
<?php header(Refresh: 5;url=http://www.xxx.com/"); ?>

Sigh. That's not the same. There isn't even a refresh header field in
rfc 2616, and I don't see any particular reason to replicate the usual
META kludge.

The PHP equivalent of the above would be something like

<?php

header("HTTP/1.x 301 Moved Permanently");
header("Location: http://www.example.com/folder/");

?>
 
K

Kim André Akerø

chlori said:
Hi

How can I redirect visitors from www.domain.com to
www.domain.com/folder?

So, if I type www.domain.com I see www.domain.com/folder

I tried with .htaccess
Redirect permanent http://www.domain.com/
http://www.domain.com/folder/
(on one line) but it didnt't work.

Did I do something wrong?

Yes. The file to be redirected must be a file from document root, such as /.
Also, if you only use / as the file to be redirected, you'll get a get a
redirect to http://www.example.com/folder/folder/folder/folder/..., which
goes right into an infinate loop.

This way will work, though:

Redirect Permanent /index.html http://www.example.com/folder/

Or is there a way with PHP?

<?php header("HTTP/1.x 301 Moved Permanently\nLocation:
http://www.example.com/folder/"); ?>

Keep in mind that your index.php file can't contain anything before the
<?php tag, not even a space or a blank line, or you'll receive a PHP error
("headers already sent/received").
 
H

Hywel

chlori said:
Hi

How can I redirect visitors from www.domain.com to www.domain.com/folder?

So, if I type www.domain.com I see www.domain.com/folder

I tried with .htaccess
Redirect permanent http://www.domain.com/ http://www.domain.com/folder/
(on one line) but it didnt't work.

Did I do something wrong? Or is there a way with PHP?

Why are you trying to do this? Would it not be possible to simply move
whatever's in /folder up one level to /?

In PHP, just put this in the document index in /
<?
header("location:folder/");
?>
 
K

Kris

Hywel said:
In PHP, just put this in the document index in /
<?
header("location:folder/");
?>

I was recently scorned by some knowledgable people for using this
method. They claimed that the Location header requires a full URI, not a
relative one, according to some RFC of which I have forgotten the number.

Can someone comment on this?
 
H

Hywel Jenkins

I was recently scorned by some knowledgable people for using this
method. They claimed that the Location header requires a full URI, not a
relative one, according to some RFC of which I have forgotten the number.

Can someone comment on this?

This is from the PHP docs, so you're right:

<quote>
Note: HTTP/1.1 requires an absolute URI as argument to Location:
including the scheme, hostname and absolute path, but some clients
accept relative URIs. You can usually use $_SERVER['HTTP_HOST'],
$_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a
relative one yourself:

<?php
header("Location: http://" . $_SERVER['HTTP_HOST']
. dirname($_SERVER['PHP_SELF'])
. "/" . $relative_url);
?>
</quote>
 
D

David Dorward

They claimed that the Location header requires a full URI, not a
relative one, according to some RFC of which I have forgotten the number.

They would be right:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30

Some user agents (the lynx browser being an obvious example) spit warnings
at the user when you do this. I don't know any agents which fail to
compensate for this error, but its a pretty good bet that there are some
out there which don't.
 
K

Kris

I was recently scorned by some knowledgable people for using this
method. They claimed that the Location header requires a full URI, not a
relative one, according to some RFC of which I have forgotten the number.
[/QUOTE]
This is from the PHP docs, so you're right:

<quote>
Note: HTTP/1.1 requires an absolute URI as argument to Location:
including the scheme, hostname and absolute path, but some clients
accept relative URIs. You can usually use $_SERVER['HTTP_HOST'],
$_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a
relative one yourself:

[skip PHP code for constructing absolute URI from a relative one]

I heard there is another header that allows relative URI's for
redirection. Looking up the URI for RFC2616 that Dorward gave [1], it
seems to be Content-Location.

[1] http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30
(The NOTE at 14.30 mentions Content-Location)

Are there any known drawbacks for using Content-Location and a relative
URI instead of Location and an absolute URI? I would prefer to start
using Content-Location rather than retrofitting the effective, yet
bothersome PHP script that was given (let alone manually entering
absolute URI's).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,781
Messages
2,569,619
Members
45,316
Latest member
naturesElixirCBDGummies

Latest Threads

Top