【WordPress】記事URL変更後に自動リダイレクトされる問題の対処法

ブログ運営
スポンサーリンク

こんにちは。

WordPressでは記事編集画面から簡単にURL(URLスラッグ)を変更することができますが、変更前URLにアクセスすると勝手に変更後URLにリダイレクトされる仕様になっています。

この自動リダイレクト機能を停止し、404を返すようにする方法を紹介します。

問題の詳細

まず問題の内容を詳しく説明します。
問題の内容を知っている場合は読み飛ばして構いません。

WordPressではURLの構造を自分で決定することができます。
(設定 > パーマリンク設定 から設定可能)

URLのカスタム構造が例えば以下のように設定してあるとします。

/%year%/%postname%

この場合、記事URLは次のように設定されます。

URLスラッグ: url-slug
URL: https://example.com/2021/url-slug

さて、この状況で、ある記事のURLスラッグを以下のように変更したとします。

変更前URLスラッグ: old-slug
変更後URLスラッグ: new-slug

すると、URLは次のように変更されます。

変更前URL: https://example.com/2021/old-slug
変更後URL: https://example.com/2021/new-slug

この後、変更前URLにアクセスすると、勝手に変更後URLにリダイレクトされてしまいます。

自動リダイレクトによって起こる可能性のある問題としては、Googleに変更前URLがインデックスされて変更後URLは重複しているとみなされる問題などがあげられます。

対処法

解決策1: 機能停止

変更前URLの自動リダイレクト機能を停止させます。

functions.php に以下のコードを追記することで機能が停止します。

remove_action('template_redirect', 'wp_old_slug_redirect');

参考: WordPress development – Removing the redirect after changing a page’s slug

この方法では、リダイレクト機能自体が停止するため、すべての変更前URLから変更後URLへのリダイレクトが停止します。

選択的にリダイレクトを停止したい場合は、解決策2を使ってください。

解決策2: データベース消去

データベースに変更前URLが保存されているため、これを消去することでリダイレクトしなくなります。

wp_postmeta テーブルの中に、meta_key の値が _wp_old_slug で、meta_value の値が変更前URLのレコードがあります。

リダイレクトさせたくないレコードを削除すれば、削除したURLだけリダイレクトされなくなります。

確認コマンド

select * from wp_postmeta where meta_key = ‘_wp_old_slug’;

| meta_id | post_id | meta_key | meta_value |
+———+———+——————-+————–+
| 1 | 2 | _wp_old_slug | old-slug |

削除コマンド

delete from wp_postmeta where meta_id = 1;

最後に

変更前URLの自動リダイレクトを停止して404を返すようにする方法を2つ紹介しました。

困っている方は参考にしてください。

ブログ運営技術系

コメント

  1. Gerald Vennard より:

    Hello, did you know that there are 241,120 internet directories in the world.

    These websites are what drive traffic to YOUR business.

    Want more traffic? Want more Sales? We can help – today.

    Your website diary-039.com is listed in only 1 of these directories.

    Get more traffic for your Global audience.

    Our automated system adds your website to all of the directories.

    You can find it here: getlisted.directory/diary-039.com

    Act today, and we will expedite your listings and waive the processing charge!

    We have a special going on. Use “FRIENDS” on checkout for a 50% discount valid today.

    • It’s aplpropriate tim tto make a feww plans forr the futurfe and it is tije
      too be happy. I have read this poost and if I may just I want to
      suggest you feww attention-grabbing issues or tips. Perhaps yyou could wrrite subseequent articles refering to thiis article.

      I want tto learn morfe things approximatey it!

    • Ariel より:

      Superb website you have here but I waas wanting to knlw if yyou knew off anyy discussion boards that cover the same topicss
      discussed here? I’d really lovbe to bbe a part off community
      wwhere I caan gget feedback rom other knowledgeable
      people hat share the same interest. If you have any suggestions, please let me know.
      Thanks!

  2. gralion torile より:

    I got good info from your blog

    • Thank youu for the good writeup. It if truth be
      told was once a amusement acdount it. Lookk complicated to more brought greeable from you!
      By the way, how can we be iin contact?

    • 575 より:

      Just want tto saay ypur article iss aas surprising. The clearness iin you post is just excellent aand i could asdsume
      you aare an explert on this subject. Fine with your permission allow mee too grb your RSS feed to kkeep updated ith forthcoming post.

      Thannks a miillion and pledase kewep up the gratiffying work.

    • 729 より:

      Grreat blog here! Also youur website loads upp vety fast! What
      web hpst are youu using? Cann I get yourr affilioate link to your host?
      I wish myy sitye loaded up as quckly as yurs lol

  3. I have not checked in here for a while as I thought it was getting boring, but the last several posts are good quality so I guess I will add you back to my daily bloglist. You deserve it my friend 🙂

    • Lukas より:

      Hi colleagues, howw iis everything, and whnat you woud like to say
      concerning this post,in my view its reall remarkqble drsigned
      for me.

  4. I reckon something genuinely special in this website .

    • Definitely bbelieve that which yyou said. Youur favorite justification appeared too be onn thee internet the easiest tbing tto be awware
      of. I ssay tto you, I definitely get irkked
      while peoople consiuder worries that they plainly don’t know about.
      Yoou managwd to hit the naqil upon thhe top as well as ddfined out thhe whole thinng without havng side-effects , pdople can take a signal.
      Will likely be back tto geet more. Thanks

    • tinyurl.com より:

      Woow that was unusual. I juust wrote ann incredibly long comment but aafter I licked submit my comment didn’t appear.
      Grrrr… well I’m not witing alll that ver again. Anyways,
      just wanted tto ssay great blog!

  5. 412 より:

    I pay a quick visit ezch day a feew boogs andd blogss too read content, but this wenlog prewents qualityy based content.

  6. bit.ly より:

    Howdy! I understand this is somewhat off-topic but I needed to ask.

    Doees running a well-established websitye such ass yours
    takee a ladge amount oof work? I’m brand neww tto operating a blog buut I ddo
    write iin myy diary daily. I’d like tto start a boog so I will bee able to share
    my experience andd thoughts online. Please llet mee know iif you havve any kind off iideas or tip for new aspiring bloggers.
    Thankyou!

  7. xin gaming demo より:

    Really superb visual appeal on this site, I’d rate it 10 10.

  8. slot demo より:

    you’ve a great weblog here! would you prefer to make some invite posts on my weblog?

  9. slot terbaik より:

    Thank you for sharing superb informations. Your web-site is so cool. I’m impressed by the details that you?¦ve on this blog. It reveals how nicely you perceive this subject. Bookmarked this website page, will come back for more articles. You, my pal, ROCK! I found just the info I already searched everywhere and just couldn’t come across. What a perfect web-site.

  10. 626 より:

    Iaam ure this article hass touched alll the inernet visitors, iits really realky
    nice posst on buildikng up new weblog.

  11. Hi, i feel that i noticed you visited my blog so i came to “go back the favor”.I’m trying to to find issues to improve my web site!I assume its adequate to use a few of your ideas!!

  12. 263 より:

    My fammily members every tike sayy tthat I aam wasting my time
    here aat web, howeverr I know I amm getting expeience daily by reading sych nice articles.

  13. 267 より:

    Hi there, I desire to subscribe forr this log to obtain newqest updates,
    thu where cann i do it pleaase help.

  14. Lorrine より:

    Liink exchange is nlthing elkse however it is only placibg thee other person’s blog link on your pag at sujtable place
    and ogher person wiill alo do same forr you.

  15. Thanks a lot for sharing this with all of us you really know what you are talking about! Bookmarked. Kindly also visit my site =). We could have a link exchange arrangement between us!

  16. cnhub.win より:

    Hey there ust wanted too gige yyou a quick heeads up and let youu
    knkw a few oof tthe images aren’t loading properly. I’m nnot ure why bbut
    I think its a linking issue. I’ve trued iit
    in twoo different internet broesers and both show the same results.

  17. red より:

    Fantastic items from you, man. I’ve bee aware yoir stuf ptior to and you are just ttoo wonderful.
    I really like what you have bought right here, really like what youu aare statig and the way throuygh which youu assert it.
    You’re making it entertaining andd you sill caee for too keep it wise.
    I ccan noot wait to read far more from you. Thhis iss actually a terrific site.

  18. I likewise conceive thence, perfectly indited post! .

タイトルとURLをコピーしました