Last active
September 20, 2018 14:23
-
-
Save TeemuSuoranta/3ead49d11aca28fab1e71aa6038c7b93 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Redirect .html URLs that cause 404 | |
*/ | |
add_action('template_redirect', function() { | |
if (is_404()) { | |
// get url without GET parameters | |
$url = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); | |
// check if current url ends with .html | |
if (preg_match('/' . preg_quote('.html', '/') . '$/', $url)) { | |
// remove .html and redirect | |
if (function_exists('mb_substr') && function_exists('mb_strlen') && function_exists('mb_substr_count')) { | |
// prevent making costly DoS requests with chaining extension | |
if (mb_substr_count($url, '.html') > 2) { | |
return; | |
} | |
$url = mb_substr($url, 0, mb_strlen($url) - 5); | |
wp_redirect($url); | |
exit(); | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment