Log in

View Full Version : Permanent Redirects



Hasan
07-31-2007, 02:39 PM
I have a site with 100 pages indexed but I want to change the overall design and structure of the site. The URL's will be totally different

Is there any script or function in PHP which I can use to redirect them permanently to the new URL's?

james438
07-31-2007, 04:35 PM
I'm just piggy backing a question on here as well: what does it mean to have 100 pages indexed? mostly curious about the word index...

Just to put in my 2 cents worth as well, when password protecting a page I often use the following that is placed at the top of my page which will redirect a person to an alternate page of my choice if they are not logged in:
<?php
session_start();
if ( @$_SESSION['pasword'] != "tweetybird" )
{
header("location: ../login.php");
exit();
}
?>


Not sure if that is what you are looking for though.

Twey
07-31-2007, 04:43 PM
header("location: ../login.php");The Location header must be an absolute URI:
header('Location: http://www.example.com/path/to/login.php');

Hasan, PHP isn't your best option for this. You can do it, but you'd need to create a page for every old page you want to redirect; hardly an efficient solution. Instead, you might want to write a .htaccess file with a redirect:
Redirect 301 /page1.html http://www.example.com/page2.html

james438
07-31-2007, 05:27 PM
The Location header must be an absolute URI:

why? The code I posted I actually copied from one of my files. I added the ../ just to shorten it a bit for simplicity and to redirect it to the parent directory. Then again if the redirect is toa completly different website the the url would have to be absolute as well.

djr33
07-31-2007, 07:15 PM
Local paths work, but they aren't standard and may not work in all browsers. "Use at your own risk", I suppose.

Twey
07-31-2007, 08:02 PM
RFC 2616, section 14.30 Location:
The Location response-header field is used to redirect the recipient to a location other than the Request-URI for completion of the request or identification of a new resource. For 201 (Created) responses, the Location is that of the new resource which was created by the request. For 3xx responses, the location SHOULD indicate the server's preferred URI for automatic redirection to the resource. The field value consists of a single absolute URI.

james438
08-01-2007, 12:38 AM
Thanks for explaining. I didn't know that. Some of what is in that quote I didn't really understand, but that would be a question for a separate thread. However, why am I able to go to a parent directory in that way? What is the original, or correct, purpose of the ../ ?

Twey
08-01-2007, 05:05 AM
What is the original, or correct, purpose of the ../ ?Exactly as you have used it: to access a directory above the current one. There's no problem with this, even in absolute URLs: you can quite happily do something like http://www.example.com/lorem/ipsum/dolor/sit/../amet.html to access http://www.example.com/lorem/ipsum/dolor/amet.html. The .. construct itself has no bearing on the issue I'm addressing here.
why am I able to go to a parent directory in that way?I'm not sure this is exactly the question you were asking, but you are able to use relative URLs in Location fields because a lot of browsers have implemented this as a feature. However, it is still non-standard, and as such cannot be relied upon.

Hasan
08-02-2007, 10:27 AM
What else can be done to get rid of it then?

Twey
08-02-2007, 04:51 PM
Get rid of it? You don't need to get rid of it, just use an absolute URL.