Home > front end >  How to Redirect a Webpage with .html to another
How to Redirect a Webpage with .html to another

Time:05-21

I previously made my website with WordPress. I have a few posts which seem to doing fine (SEO). The url of my posts are www.sitename.com/this-is-post-title. I have rebuilt the the site with html, and planning on moving the blog posts to blog.sitename.com so that the new url will be blog.sitename.com/this-is-post-title.

I have a created an html file as this-is-post-title.html and set the redirect to blog.sitename.com/this-is-post-title. this is the redirect code

    <meta http-equiv = "refresh" content = "0; url = https://blog.sitename.com/this-is-post-title/" />

It works fine when sitename.com/this-is-post-title.html is accessed, but not when sitename.com/this-is-post-title is accessed. My previous post is also without .html since I was working in WordPress. Any help for me?

CodePudding user response:

If it is a wordpress website you can use this piece of code to redirect to another page. Paste it under functions.php

function redirect_page() {

 if (isset($_SERVER['HTTPS']) &&
    ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
    isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
    $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
    $protocol = 'https://';
    }
    else {
    $protocol = 'http://';
}

$currenturl = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$currenturl_relative = wp_make_link_relative($currenturl);

switch ($currenturl_relative) {

    case '[from slug]':
        $urlto = home_url('[to slug]');
        break;
    
    default:
        return;

}

if ($currenturl != $urlto)
    exit( wp_redirect( $urlto ) );


}
add_action( 'template_redirect', 'redirect_page' );

Change [from slug] and [to slug] with your urls

CodePudding user response:

You can add this line in your .htaccess file:

Options  Multiviews

Multiviews is a feature in Apache that will serve content from URLs without an extension by searching for similarly named files with an extension.

Alternately, you could implement redirects in .htaccess

RedirectMatch permanent ^/this-is-post-title$ /this-is-post-title.html

OR

RewriteEngine on
RewriteRule ^/this-is-post-title$ /this-is-post-title.html [R=301,L]
  • Related