| @@ -41,20 +41,47 @@ | ||
| 41 | 41 | return $redirect_url; |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | public function template_redirect() { |
| 45 | - if ( is_404() ) { | |
| 46 | - $options = red_get_options(); | |
| 45 | + if ( ! is_404() || $this->matched ) { | |
| 46 | + return; | |
| 47 | + } | |
| 47 | 48 | |
| 48 | - if ( isset( $options['expire_404'] ) && $options['expire_404'] >= 0 && apply_filters( 'redirection_log_404', $this->can_log ) ) { | |
| 49 | - RE_404::create( Redirection_Request::get_request_url(), Redirection_Request::get_user_agent(), Redirection_Request::get_ip(), Redirection_Request::get_referrer() ); | |
| 50 | - } | |
| 49 | + if ( $this->match_404_type() ) { | |
| 50 | + // Don't log an intentionally redirected 404 | |
| 51 | + return; | |
| 51 | 52 | } |
| 53 | + | |
| 54 | + $options = red_get_options(); | |
| 55 | + | |
| 56 | + if ( isset( $options['expire_404'] ) && $options['expire_404'] >= 0 && apply_filters( 'redirection_log_404', $this->can_log ) ) { | |
| 57 | + RE_404::create( Redirection_Request::get_request_url(), Redirection_Request::get_user_agent(), Redirection_Request::get_ip(), Redirection_Request::get_referrer() ); | |
| 58 | + } | |
| 52 | 59 | } |
| 53 | 60 | |
| 61 | + private function match_404_type() { | |
| 62 | + if ( ! property_exists( $this, 'redirects' ) || count( $this->redirects ) === 0 ) { | |
| 63 | + return false; | |
| 64 | + } | |
| 65 | + | |
| 66 | + $page_type = array_values( array_filter( $this->redirects, function( $redirect ) { | |
| 67 | + return $redirect->match->get_type() === 'page'; | |
| 68 | + } ) ); | |
| 69 | + | |
| 70 | + if ( count( $page_type ) > 0 ) { | |
| 71 | + $url = apply_filters( 'redirection_url_source', Redirection_Request::get_request_url() ); | |
| 72 | + $first = $page_type[0]; | |
| 73 | + $first->matches( $url ); | |
| 74 | + return true; | |
| 75 | + } | |
| 76 | + | |
| 77 | + return false; | |
| 78 | + } | |
| 79 | + | |
| 80 | + // Return true to stop further processing of the 'do nothing' | |
| 54 | 81 | public function redirection_do_nothing() { |
| 55 | 82 | $this->can_log = false; |
| 56 | - return false; | |
| 83 | + return true; | |
| 57 | 84 | } |
| 58 | 85 | |
| 59 | 86 | public function redirection_visit( $redirect, $url, $target ) { |
| 60 | 87 | $redirect->visit( $url, $target ); |
| @@ -86,8 +113,13 @@ | ||
| 86 | 113 | } |
| 87 | 114 | } |
| 88 | 115 | |
| 89 | 116 | do_action( 'redirection_last', $url, $this ); |
| 117 | + | |
| 118 | + if ( ! $this->matched ) { | |
| 119 | + // Keep them for later | |
| 120 | + $this->redirects = $redirects; | |
| 121 | + } | |
| 90 | 122 | } |
| 91 | 123 | } |
| 92 | 124 | |
| 93 | 125 | /** |
| @@ -93,9 +125,9 @@ | ||
| 93 | 125 | /** |
| 94 | 126 | * Protect certain URLs from being redirected. Note we don't need to protect wp-admin, as this code doesn't run there |
| 95 | 127 | */ |
| 96 | 128 | private function protected_url( $url ) { |
| 97 | - $rest = parse_url( red_get_rest_api() ); | |
| 129 | + $rest = wp_parse_url( red_get_rest_api() ); | |
| 98 | 130 | $rest_api = $rest['path'] . ( isset( $rest['query'] ) ? '?' . $rest['query'] : '' ); |
| 99 | 131 | |
| 100 | 132 | if ( substr( $url, 0, strlen( $rest_api ) ) === $rest_api ) { |
| 101 | 133 | // Never redirect the REST API |
| @@ -135,9 +167,9 @@ | ||
| 135 | 167 | if ( $status === 301 && php_sapi_name() === 'cgi-fcgi' ) { |
| 136 | 168 | $servers_to_check = array( 'lighttpd', 'nginx' ); |
| 137 | 169 | |
| 138 | 170 | foreach ( $servers_to_check as $name ) { |
| 139 | - if ( stripos( $_SERVER['SERVER_SOFTWARE'], $name ) !== false ) { | |
| 171 | + if ( isset( $_SERVER['SERVER_SOFTWARE'] ) && stripos( $_SERVER['SERVER_SOFTWARE'], $name ) !== false ) { | |
| 140 | 172 | status_header( $status ); |
| 141 | 173 | header( "Location: $url" ); |
| 142 | 174 | exit( 0 ); |
| 143 | 175 | } |