ENHANCEMENT: Ticket #2382 - Merged patch changing 302 for /home to 301

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@53177 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sean Harvey 2008-04-22 01:45:55 +00:00
parent 3f50c7a477
commit 4a5443c5da
4 changed files with 18 additions and 8 deletions

View File

@ -88,7 +88,7 @@ class ContentController extends Controller {
unset($getVars['url']); unset($getVars['url']);
if($getVars) $url = "?" . http_build_query($getVars); if($getVars) $url = "?" . http_build_query($getVars);
else $url = ""; else $url = "";
Director::redirect($url); Director::redirect($url, 301);
return; return;
} }

View File

@ -509,7 +509,7 @@ class Controller extends ViewableData {
* Redirct to the given URL. * Redirct to the given URL.
* It is generally recommended to call Director::redirect() rather than calling this function directly. * It is generally recommended to call Director::redirect() rather than calling this function directly.
*/ */
function redirect($url) { function redirect($url, $code=302) {
if($this->response->getHeader('Location')) { if($this->response->getHeader('Location')) {
user_error("Already directed to " . $this->response->getHeader('Location') . "; now trying to direct to $url", E_USER_ERROR); user_error("Already directed to " . $this->response->getHeader('Location') . "; now trying to direct to $url", E_USER_ERROR);
} }
@ -519,7 +519,7 @@ class Controller extends ViewableData {
$url = Director::baseURL() . $url; $url = Director::baseURL() . $url;
} }
$this->response->redirect($url); $this->response->redirect($url, $code);
} }
/** /**

View File

@ -285,8 +285,8 @@ class Director {
* - or it can be a URL relative to the "site base" * - or it can be a URL relative to the "site base"
* - if it is just a word without an slashes, then it redirects to another action on the current controller. * - if it is just a word without an slashes, then it redirects to another action on the current controller.
*/ */
static function redirect($url) { static function redirect($url, $code=302) {
Controller::curr()->redirect($url); Controller::curr()->redirect($url, $code);
} }
/** /**

View File

@ -53,6 +53,15 @@ class HTTPResponse extends Object {
505 => 'HTTP Version Not Supported', 505 => 'HTTP Version Not Supported',
); );
protected static $redirect_codes = array(
301,
302,
303,
304,
305,
307
);
protected $statusCode = 200; protected $statusCode = 200;
protected $headers = array(); protected $headers = array();
protected $body = null; protected $body = null;
@ -91,8 +100,9 @@ class HTTPResponse extends Object {
} }
} }
function redirect($dest) { function redirect($dest, $code=302) {
$this->statusCode = 302; if(!in_array($code, self::$redirect_codes)) $code = 302;
$this->statusCode = $code;
$this->headers['Location'] = $dest; $this->headers['Location'] = $dest;
} }
@ -100,7 +110,7 @@ class HTTPResponse extends Object {
* Send this HTTPReponse to the browser * Send this HTTPReponse to the browser
*/ */
function output() { function output() {
if($this->statusCode == 302 && headers_sent($file, $line)) { if(in_array($this->statusCode, self::$redirect_codes) && headers_sent($file, $line)) {
$url = $this->headers['Location']; $url = $this->headers['Location'];
echo echo
"<p>Redirecting to <a href=\"$url\" title=\"Please click this link if your browser does not redirect you\">$url... (output started on $file, line $line)</a></p> "<p>Redirecting to <a href=\"$url\" title=\"Please click this link if your browser does not redirect you\">$url... (output started on $file, line $line)</a></p>