๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Lect & Tip/javascript, jQuery

-์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ link Canonical javascript createElement appendChild window.location.href

by ๋‚ฏ์„ ๊ณต๊ฐ„2019 2019. 8. 26.

๋ชฉ์ฐจ

    ๊ฒฐ๋ก ๋ถ€ํ„ฐ ๋งํ•˜์ž๋ฉด ์‚ฝ์งˆ์ด๋‹ค.

    ์–ด๋Š๋‚  ๊ฐ‘์ž๊ธฐ ๊ตฌ๊ธ€ ์• ๋“œ์„ผ์Šค ๊ด‘๊ณ ๊ฐ€ ๋œจ์ง€ ์•Š๊ธธ๋ž˜ ํ™•์ธํ•ด ๋ณด๋‹ˆ ๊ฐœ์ธ๋„๋ฉ”์ธ์œผ๋กœ ์„ค์ •๋œ ๋ฌด๋ฃŒ ๋„๋ฉ”์ธ์˜ ๋ฃจํŠธ ๋„๋ฉ”์ธ์—์„œ ๊ตฌ๊ธ€ ์• ๋“œ์„ผ์Šค ์ •์ฑ… ์œ„๋ฐ˜์ด ๋–ด๋‹ค.

    ๋ฌด๋ฃŒ ๋„๋ฉ”์ธ์€ ๋‚ด๋„๋ฉ”์ธ.ํ•œ๊ตญ ์˜ ์ฃผ์†Œ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

    ์ด๊ฒŒ ํ•ด๊ฒฐ์ด ์•ˆ๋˜๋ฉด ๊ด‘๊ณ ๋ฅผ ๊ฒŒ์žฌํ•  ์ˆ˜๊ฐ€ ์—†๋‹จ๋‹ค.

    ๋‚œ๊ฐํ•˜๊ฒŒ๋„...์ด ๋„๋ฉ”์ธ ์ฃผ์†Œ๋กœ ๋„ค์ด๋ฒ„ ์œ ์ž…์ด ์ œ๋ฒ• ์žˆ๋‹ค.

    ๊ทธ๋ž˜์„œ ํ˜น์‹œ๋‚˜ 301 redirection์ด๋ผ๋„ ์จ ๋ณผ๊นŒ ํ–ˆ์ง€๋งŒ ๊ทธ๋Ÿด ๋ฐฉ๋ฒ•์ด ์ „ํ˜€ ์—†๋‹ค.

    ๋ˆ์„ ์“ฐ๋ฉด ๊ฐ€๋Šฅํ•˜๊ฒ ์ง€๋งŒ...

    ์ผ๋‹จ ์ƒˆ๋กœ ํ˜ธ์ŠคํŒ…์„ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค๋Š” ์ „์ œ

    ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ link Canonical javascript createElement appendChild window.location.href

    ํ•˜์— ๊ทธ๋ ‡๋‹ค.

    ๊ทธ๋ž˜์„œ ๊ถ๋ฆฌํ•œ ๊ฒƒ์ด ๋‚ด ๋„๋ฉ”์ธ ํ•œ๊ตญ์˜ ๋‹จ์ผํŽ˜์ด์ง€ ์ˆ˜์ •์—์„œ Canonical๋ฅผ ์„ค์ •ํ•ด์ฃผ๊ณ , ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ location.href๋กœ ๋ฆฌ๋‹ค์ด๋ ‰์…˜์„ ๊ฑธ์–ด์„œ ์˜ฎ๊ธฐ๋Š” ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ•ด ๋ดค๋‹ค.

    ๊ฒฐ๊ณผ๋Š” ์•ž์„œ ๋งํ•œ ๋ฐ๋กœ ์‚ฝ์งˆ์ด๊ณ  ์‹คํŒจ๋‹ค.

    ๋ฌผ๋ก  ํ›Œ๋ฅญํ•˜๊ฒŒ link ์—˜๋ฆฌ๋ฉ˜ํŠธ๋„ ์ƒ์„ฑ์ด ๋˜๊ณ  ํŽ˜์ด์ง€๋ณ„๋กœ Canonical์ด ์ƒ์„ฑ๋˜์—ˆ๋‹ค.

    ๋ฌธ์ œ๋Š” ๋‚ด๋„๋ฉ”์ธ.ํ•œ๊ตญ์˜ ์ฃผ์†Œ์— SSL ์ธ์ฆ์„œ ์„ค์ •์ด ๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ๋„ค์ด๋ฒ„๋‚˜ ๊ตฌ๊ธ€์—์„œ SSL์˜ ์ฃผ์†Œ๋กœ ๋ณด๋‚ด๋Š” ๋งํฌ๋ฅผ ์ด ๋„๋ฉ”์ธ์ด ๋ฐ›์ง€๋ฅผ ๋ชปํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

    ์ด ๋งํฌ๋ฅผ ๋ฐ›์œผ๋ ค๋ฉด SSL ์„ค์ •์ด ๋œ ํ˜ธ์ŠคํŒ…์„ ๋ฐ›์•„์•ผํ•œ๋‹ค.

    ํ˜ธ์ŠคํŒ…์„ ๋ฐ›์„ ๊ฒƒ์ด๋ผ๋ฉด ์ด ๋ฐฉ๋ฒ•์„ ์“ฐ์ง€ ์•Š๊ณ  .htacess ํŒŒ์ผ์—์„œ moved 301 redirection์„ ๊ฑธ์–ด ์ฃผ๋Š” ๊ฒƒ์ด ์˜ฌ๋ฐ”๋ฅธ ๋ฐฉ๋ฒ•์ด๋‹ค.

    <html>

    <head>

    <script>

    var newURL = "exocet2015.tistory.com";

    var oldURL = "exocet2015.f4m.kr";

    var thisURL = location.href;

    var renewURL = thisURL.replace(oldURL,newURL);

    var renewURL = renewURL .replace("http:","https:");

    var linkCanonical= document.createElement("link");

    linkCanonical.setAttribute("rel", "canonical");

    linkCanonical.setAttribute("href", renewURL);

    document.head.appendChild(linkCanonical);

    window.location.href = renewURL;

    </script>

    </head>

    <body>

    </body>

    </html>

    ์•„๋ฌดํŠผ ํ˜ผ์‹œ ์ฐจํ›„์—๋ผ๋„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ํ—ค๋”์— ํƒœ๊ทธ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด ์œ„์˜ ์†Œ์Šค๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด ๋œ๋‹ค.

    ํ•ต์‹ฌ์€ createElement๋ฅผ ์ด์šฉํ•ด์„œ element ํƒœ๊ทธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ ํ•จ์ˆ˜์ธ {{string}}.replace('a','b');

    {{string}}์˜ ๋ฌธ์ž์—ด์—์„œ a๋ฅผ ์ฐพ์•„์„œ b๋กœ ๋ฐ”๊พผ ๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.

    ์ด ๋•Œ Attribute๋Š” setAttirbuteํ•จ์ˆ˜๋กœ ์žก๋Š”๋‹ค.

    ๋งŒ๋“ค์–ด์ง„ ๊ฐ์ฒด๋Š” appendChild ํ•จ์ˆ˜๋กœ ์ ์ ˆํ•œ ์œ„์น˜์— ์ถ”๊ฐ€ํ•œ๋‹ค.

    head.appendChild(ElementObj)๋ฅผ ์“ฐ๋ฉด ํ—ค๋”์— ๊ฐ€์„œ ๋ถ™๊ณ , body.appendChild(ElementObj)ํ•˜๋ฉด ๋‚ด์šฉ ์–ด๋”˜๊ฐ€์— ๊ฐ€์„œ ๋ถ™๋Š”๋‹ค.

    #์•„์ด๋””๋ฅผ ์„ค์ •ํ•˜๋ฉด ์•„์ด๋”” ๊ฐ์ฒด์— ๋ถ™์ผ ์ˆ˜ ์žˆ๋‹ค.

    ํ—ฌ๋กœ๋น„๋„ˆ์Šค ๋‚˜๋ผ ๋ชธ๋งค ๋ฏธ๋‹ˆ์Šค์ปคํŠธ ๊ฐ์„ ๋ฏธ ๋Œ€๋ฐ•ํ—ฌ๋กœ๋น„๋„ˆ์Šค ๋‚˜๋ผ ๋ชธ๋งค ๋ฏธ๋‹ˆ์Šค์ปคํŠธ ๊ฐ์„ ๋ฏธ ๋Œ€๋ฐ•

    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€