티스토리에서 워드프레스로 블로그 이전 후 Redirect

Published Aug 30, 2022 | Updated Sep 23, 2022 | 0 comments

WordPress로 이전 한지 꽤 시간이 지났음에도 아직도 예전 블로그 글들이 오래전에 작성된 거라 더 노출이 잘 되는듯 하다

그래서 이전 블로그 조회시 현재 블로그에 동일한 글이 있는 경우 Page 를 Redirect 하게 끔 수정해 보았다.

두 사이트 모두 permalink 를 동일한 형식으로 사용중이고, 이전 사이트 에서 마이그레이션(링크 참조) 한 상태라

일부러 삭제한 글을 제외하면 동일한 경로에 존재한다.

현재 WordPress 사이트에 CORS 설정이 필요하다

Origin 은 정해져 있고, GET Method 를 사용해서 접근할 예정이라 아래 코드를 테마 파일의 function.php 파일에 추가해 주었다.

// BEGIN TISTORY CORS
add_action('init', 'handle_tistory_redirect');
function handle_tistory_redirect() {
    $origin = get_http_origin();
    if ($origin === 'https://kkomzi.tistory.com') {
    	header("Access-Control-Allow-Origin: https://kkomzi.tistory.com");
        header("Access-Control-Allow-Methods: GET, OPTIONS");
        header("Access-Control-Allow-Credentials: true");
        header('Access-Control-Allow-Headers: *');
        if ('OPTIONS' == $_SERVER['REQUEST_METHOD']) {
            status_header(200);
            exit();
        }
    }
}
// END TISTORY CORS

이전 사이트의 Head 태그 사이에 아래 Javascript 코드를 추가한다.

간단하게 메인 페이지가 아닌 경우 Pathname 을 사용해서 response.ok 인 경우만 redirect 하는 코드이다.

태그 및 카테고리 페이지를 위해서는 조건 추가가 필요하겠지만, 그냥 이쯤에서 만족하는 걸로…..

<script type="text/javascript">
function redirect() {
	var url = new URL(window.location.href);
	if(url.pathname.substring(0) != '/') {
		var newUrl = "https://blog.kkomzi.net" + url.pathname.substring(0);
		fetch(newUrl, {
			method: 'GET'
		})
		.then((response) => {
			if(response.ok) {
				//window.location.replace(newUrl);
				window.location.href = newUrl;
			} else {
				console.log(response);
			}
		})
		.catch((error) => console.log(error))
		;
	}
}
</script>

그리고 Body 태그의 onload 이벤트에서 해당 function 을 호출합니다.

<body onload="redirect()">

TAG INFORMATION

Learn more on this topic

Related Blog Posts

워드프레스 Github 플러그인

워드프레스 Github 플러그인

워드프레스에서 Github 저장소를 포함하기 위해 사용 중이던 Embed Block for GitHub 플러그인의 버그 발생과 더 이상 유지 보수가 안되는 상황에서 새로운 플러그인(Github Embed)을 소개하고 해당 플러그인으로 대체 하게 된 상황에 대해 설명합니다.

read more

Join in the conversation

Leave a Comment

0 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

무료 온라인 전광판

전광판

텍스트를 입력하고 텍스트 효과 및 배경효과 를 변경해서 전체화면으로 표시할 수 있는 전광판 용도로 사용하실 수 있습니다. 각종 스포츠 및 공연 관람시 응원 용도로 사용이 가능합니다.

Carousel

여러개의 슬라이드를 추가하여 프레젠테이션 및 이미지 슬라이드 용도로 사용하실 수 있습니다. 브라우저가 포함된 IT 기기로 큰 모니터에 연결하여 매장 내 공지사항 및 메뉴소개를 이미지로 표시할 수 있습니다.

Pin It on Pinterest

Shares
Share This

Share This

Share this post with your friends!

Shares