Skip to content

Instantly share code, notes, and snippets.

@TeemuSuoranta
Last active September 20, 2018 14:23
Show Gist options
  • Save TeemuSuoranta/3ead49d11aca28fab1e71aa6038c7b93 to your computer and use it in GitHub Desktop.
Save TeemuSuoranta/3ead49d11aca28fab1e71aa6038c7b93 to your computer and use it in GitHub Desktop.
/**
* 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