From d39d7e2347a7c850d2a3ca2879fac907e9d9a2e5 Mon Sep 17 00:00:00 2001 From: rboren Date: Fri, 27 Aug 2004 20:59:38 +0000 Subject: [PATCH] 404 permalink handler. git-svn-id: http://svn.automattic.com/wordpress/trunk@1570 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-blog-header.php | 72 ++++++++++++++++++++++++++------------- wp-includes/classes.php | 8 ++++- wp-includes/functions.php | 6 ++++ 3 files changed, 62 insertions(+), 24 deletions(-) diff --git a/wp-blog-header.php b/wp-blog-header.php index 1b7b6d7a8a..6a7286df76 100644 --- a/wp-blog-header.php +++ b/wp-blog-header.php @@ -8,34 +8,48 @@ require_once( dirname(__FILE__) . '/wp-config.php'); require_once( dirname(__FILE__) . '/wp-includes/wp-l10n.php'); -// Process PATH_INFO, if set. -$path_info = array(); -if ( !empty( $_SERVER['PATH_INFO'] ) ) { +$query_vars = array(); + +// Process PATH_INFO and 404. +if ((isset($_GET['error']) && $_GET['error'] == '404') || + (! empty( $_SERVER['PATH_INFO']))) { + // Fetch the rewrite rules. $rewrite = rewrite_rules('matches'); - $pathinfo = $_SERVER['PATH_INFO']; - // Trim leading '/'. - $pathinfo = preg_replace('!^/!', '', $pathinfo); - if (! empty($rewrite)) { - // Get the name of the file requesting path info. - $req_uri = $_SERVER['REQUEST_URI']; + $pathinfo = $_SERVER['PATH_INFO']; + $req_uri = $_SERVER['REQUEST_URI']; + $home_path = parse_url(get_settings('home')); + $home_path = $home_path['path']; + + // Trim path info from the end and the leading home path from the + // front. For path info requests, this leaves us with the requesting + // filename, if any. For 404 requests, this leaves us with the + // requested permalink. $req_uri = str_replace($pathinfo, '', $req_uri); - $req_uri = preg_replace("!/+$!", '', $req_uri); - $req_uri = explode('/', $req_uri); - $req_uri = $req_uri[count($req_uri)-1]; + $req_uri = str_replace($home_path, '', $req_uri); + $req_uri = trim($req_uri, '/'); + $pathinfo = trim($pathinfo, '/'); + + // The requested permalink is in $pathinfo for path info requests and + // $req_uri for other requests. + if (! empty($pathinfo)) { + $request = $pathinfo; + } else { + $request = $req_uri; + } // Look for matches. - $pathinfomatch = $pathinfo; + $request_match = $request; foreach ($rewrite as $match => $query) { - // If the request URI is the anchor of the match, prepend it + // If the requesting file is the anchor of the match, prepend it // to the path info. - if ((! empty($req_uri)) && (strpos($match, $req_uri) === 0)) { - $pathinfomatch = $req_uri . '/' . $pathinfo; - } + if ((! empty($req_uri)) && (strpos($match, $req_uri) === 0)) { + $request_match = $req_uri . '/' . $request; + } - if (preg_match("!^$match!", $pathinfomatch, $matches)) { + if (preg_match("!^$match!", $request_match, $matches)) { // Got a match. // Trim the query of everything up to the '?'. $query = preg_replace("!^.+\?!", '', $query); @@ -44,25 +58,32 @@ if ( !empty( $_SERVER['PATH_INFO'] ) ) { eval("\$query = \"$query\";"); // Parse the query. - parse_str($query, $path_info); + parse_str($query, $query_vars); + + // If we're processing a 404 request, clear the error var + // since we found something. + if (isset($_GET['error'])) { + unset($_GET['error']); + } + break; } } - } + } } -$wpvarstoreset = array('m','p','posts','w', 'cat','withcomments','s','search','exact', 'sentence','poststart','postend','preview','debug', 'calendar','page','paged','more','tb', 'pb','author','order','orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'feed', 'author_name', 'static', 'pagename'); +$wpvarstoreset = array('m','p','posts','w', 'cat','withcomments','s','search','exact', 'sentence','poststart','postend','preview','debug', 'calendar','page','paged','more','tb', 'pb','author','order','orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'feed', 'author_name', 'static', 'pagename', 'error'); for ($i=0; $iis_single = false; @@ -37,6 +38,7 @@ class WP_Query { $this->is_search = false; $this->is_feed = false; $this->is_home = false; + $this->is_404 = false; unset($this->posts); unset($this->query); @@ -160,12 +162,16 @@ class WP_Query { $this->is_page = true; } + if ('' != $qv['error'] || '404' == $qv['pagename']) { + $this->is_404 = true; + } + if ( ($this->is_date || $this->is_author || $this->is_category) && (! ($this->is_single || $this->is_page)) ) { $this->is_archive = true; } - if ( ! ($this->is_archive || $this->is_single || $this->is_page || $this->is_search || $this->is_feed)) { + if ( ! ($this->is_archive || $this->is_single || $this->is_page || $this->is_search || $this->is_feed || $this->is_404)) { $this->is_home = true; } } diff --git a/wp-includes/functions.php b/wp-includes/functions.php index c17ad1fc76..aa7cc92482 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -1662,6 +1662,12 @@ function is_home () { return $wp_query->is_home; } +function is_404 () { + global $wp_query; + + return $wp_query->is_404; +} + function get_query_var($var) { global $wp_query;