Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 .

Thank you

share|improve this question
    
I'm curious, why do you want to do that ? –  mike23 Jul 14 '11 at 10:14
    
i already said that..posts are indexed by Google without .html ,so they get an error . does anyone know how to do this please? –  whitelord Jul 14 '11 at 12:14
    
@whitelord I think what mike23 is trying to say is why switch from posturl to posturl.html in the first place? –  Brady Jul 14 '11 at 15:04
1  
Who told you that, an SEO expert? If it makes any difference at all i'd expect it to be marginal, at least with regard to search engines like Google. –  t31os Jul 15 '11 at 9:14
1  
I'd think if google is using the non html pages then THAT would be better SEO, not the .html? If anythign its worse SEO as google now knows never to return to check for updated content –  Tom J Nowell Nov 23 '11 at 12:03
show 4 more comments

4 Answers 4

You can edit your .htaccess directly or download an htaccess editor plugin for WordPress. Then write your rule similar to this:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*) $1.html [nc]

For more detailed instructions

share|improve this answer
    
thank you that will not redirect them .i already have the link .html ,i need a redirection rule if i enter a post without .html to redirect to .html –  whitelord Jul 16 '11 at 7:12
add comment

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.

share|improve this answer
add comment

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.html ... ONLY 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 :)

share|improve this answer
add comment

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.

share|improve this answer
    
While I agree with you that this makes no difference in SEO, I would counter that the .html extension does not imply that "the content is static and will never change." Simply put, it doesn't matter one way or another. –  Matthew Boynes Jan 23 '12 at 19:11
    
Hence implies, doesn't necessarily mean it is always static, just that it's often the case, or suggests it is –  Tom J Nowell Jan 23 '12 at 20:24
add comment

protected by toscho Jun 23 '12 at 18:01

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.