0

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?

Aimhigh
  • 3
  • 3

2 Answers2

0

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

Hamid Israfilov
  • 101
  • 1
  • 6
  • The new website which will taking the base url of the current wordpress site is not in wordpress. – Aimhigh May 21 '22 at 08:52
  • Thanks Hamid, but the new website which will taking the base url of the current wordpress site is not in wordpress. Previously, sitename.com is wordpress site. It has blog post of site.com/post-title. Now, site.com is no more of wordpress but pure coding (html). When people click on the old post from search engine (which has not .html) at the end, it will go to file not found. I am able to redirect post-title.html to blog.site.com/post-title, but not able to redirect site.com/post-title since the wordpress will not be existing anymore. – Aimhigh May 21 '22 at 09:06
  • If old wordpress website doesn't exist it is impossible to redirect it to new url with .html ending but you can remove .html ending from your new website url maybe this ( https://stackoverflow.com/questions/5730092/how-to-remove-html-from-url ) will help – Hamid Israfilov May 21 '22 at 09:17
  • Thanks Hamid, I taking a look at it. I'm so grateful – Aimhigh May 21 '22 at 09:19
0

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]
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109