Redirect all posts urls from without .html to .html

How can i redirect all posts permalinks to .html ,in WordPress ? now all my posts url are ending in .html ,but before it was without .html

to be more clear ,for example a post url like this www.example.com/posturl to be redirected to www.example.com/posturl.html .it’s simple to redirect them one by one,but i need a rule to redirect all posts .

Read More

Thank you

Related posts

Leave a Reply

4 comments

  1. You can add an extension to your Post permalinks in the WordPress Admin by going to Settings > Permalinks and creating a Custom Structure with .html at the end.

    For example:

    (•) Custom Structure: /%category%/%postname%.html
    

    This will only work with Posts, though. If you want to add .html to Pages’ permalinks, you can try a plugin like Custom Page Extensions or use the .htaccess rule posted by Bitmap.

  2. There’s a risk with the .htaccess approach. He has to write specific rules to target only posts. Not all URLs on the server.

    This is another approach. Works within WordPress, is ‘kind-of’ safer but a bit performance taxing as it needs to load the plugins before it kicks in. Theory is simple. Redirect any URL not containing . in the last segment like /uri or /uri/ to /uri.htmlONLY IF WordPress signals a is_404() error.

    Place this plugin in the /wp-content/mu-plugins/redirector.php.

    // Only here we have a valid is_404() to check if occurs.
    add_action('wp', function(){
        if(!is_404()) return; // Bail if not a 404 (WordPress has got your back)
        // Extract the URI and the Query-String (which is used later again)
        list($uri, $qs) = explode('?', $_SERVER['REQUEST_URI']);
        // Bail if current URL contains a . in the last segment of the URI
        if(!preg_match('~/[^/.]+/?$~', $uri)) return;
        // Right-trim the last ./ and append the .html to it
        $uri = rtrim($uri, '/.').".html".(!empty($qs) ? "?{$qs}" : null);
        // Redirect to the new URL (with a 301 to keep link juice flowing) and hope it works :)
        wp_redirect($uri, 301); die; // Over and out!
    });
    

    I’d rather go for a .htaccess redirect anytime but it depends. If you really messed up your blog by changing permalink structure LIVE (which should never be done when some traffic/rankings are achieved), this is a safer method as it only acts when an is_404() occurs. Fine tune it if you have issues with categories, etc.

    Warning: PHP 5.3+ Closure used. Consider reverting to PHP 5.2 syntax and compatibility… an assignment 🙂

  3. You’ve cited your doing this for SEO purposes.

    At the moment there is zero evidence, that adding .html to the end of the URL will improve Google SEO, and by your own admission, Google is indexing the non-html ending URLs instead.

    What is more, the .html extension implies the content is static and will never change, meaning Google can index it once and never return. Would it not be preferable for Google to come back? Especially considering that the ‘freshness’ of a page now plays a heavy part in how high it’s ranked.