{"id":589,"date":"2023-04-18T13:29:42","date_gmt":"2023-04-18T11:29:42","guid":{"rendered":"https:\/\/astuces-wp.com\/?p=589"},"modified":"2023-04-18T13:29:43","modified_gmt":"2023-04-18T11:29:43","slug":"redirect-prefix-wordpress","status":"publish","type":"post","link":"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/","title":{"rendered":"Automatically redirect posts with a custom prefix"},"content":{"rendered":"\n<div class=\"QmZWSe\">\n<p class=\"DHcWmd\">\u00a0<\/p>\n<\/div>\n<div class=\"oSioSc\">\n<div id=\"tw-target\">\n<div id=\"kAz1tf\" class=\"g9WsWb\">\n<div id=\"tw-target-text-container\" class=\"tw-ta-container F0azHf tw-nfl\" tabindex=\"0\">\n<p id=\"tw-target-text\" class=\"tw-data-text tw-text-large tw-ta\" dir=\"ltr\" data-placeholder=\"Traduction\"><span class=\"Y2IQFc\" lang=\"en\">In the example we need all of our articles to have the following schema: <\/span><\/p>\n<ul>\n<li class=\"tw-data-text tw-text-large tw-ta\" dir=\"ltr\" data-placeholder=\"Traduction\"><span class=\"Y2IQFc\" lang=\"en\">Site URL <\/span><\/li>\n<li class=\"tw-data-text tw-text-large tw-ta\" dir=\"ltr\" data-placeholder=\"Traduction\"><span class=\"Y2IQFc\" lang=\"en\">news <\/span><\/li>\n<li class=\"tw-data-text tw-text-large tw-ta\" dir=\"ltr\" data-placeholder=\"Traduction\"><span class=\"Y2IQFc\" lang=\"en\">post name <\/span><\/li>\n<\/ul>\n<p><span class=\"Y2IQFc\" lang=\"en\">But once the structure is changed, not all URLs are 404.<\/span><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Hook template_redirect<\/h2>\n\n\n\n<div id=\"tw-target-text-container\" class=\"tw-ta-container F0azHf tw-nfl\" tabindex=\"0\">\n<p id=\"tw-target-text\" class=\"tw-data-text tw-text-large tw-ta\" dir=\"ltr\" data-placeholder=\"Traduction\"><span class=\"Y2IQFc\" lang=\"en\">The objective of the hook is to detect when a URL responds in 404, if this is the case we look for the slug of the post and if it corresponds to a post in the database we redirect it with our prefix. <\/span><\/p>\n<p class=\"tw-data-text tw-text-large tw-ta\" dir=\"ltr\" data-placeholder=\"Traduction\"><span class=\"Y2IQFc\" lang=\"en\">It <strong>works with all prefixes<\/strong>!<\/span><\/p>\n<\/div>\n<div id=\"tw-target-rmn-container\" class=\"tw-target-rmn tw-ta-container F0azHf tw-nfl\">\n<p id=\"tw-target-rmn\" class=\"tw-data-placeholder tw-text-small tw-ta\" dir=\"ltr\">\u00a0<\/p>\n<\/div>\n\n\n\n<pre class=\"wp-block-preformatted\">function add_prefix() {\n    global $wp;\n\n    if(is_404()){\n        $url = home_url( $wp->request );\n        $segments = explode('\/', parse_url($url, PHP_URL_PATH));\n        $last_segment = end($segments);\n\n        $post_slug = get_page_by_path($last_segment, OBJECT, 'post');\n        if ($post_slug) {\n            $post_id = $post_slug->ID;\n            wp_redirect(( get_permalink($post_slug->ID) ), 301 );\n\n        } else {\n        \/\/on sait jamais\n        }\n    }\n}\nadd_filter( 'template_redirect', 'add_prefix' );<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0 In the example we need all of our articles to have the following schema: Site URL news post name But once the structure is changed, not all URLs are 404. Hook template_redirect The objective of the hook is to detect when a URL responds in 404, if this is the case we look for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":576,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68],"tags":[52],"class_list":["post-589","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-hook"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Automatically 301 posts with prefix in custom structure permalinks<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/\" \/>\n<meta property=\"og:locale\" content=\"fr_FR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Automatically 301 posts with prefix in custom structure permalinks\" \/>\n<meta property=\"og:description\" content=\"\u00a0 In the example we need all of our articles to have the following schema: Site URL news post name But once the structure is changed, not all URLs are 404. Hook template_redirect The objective of the hook is to detect when a URL responds in 404, if this is the case we look for [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-04-18T11:29:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-04-18T11:29:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/magali-colas.fr\/astuces-wordpress\/wp-content\/uploads\/2023\/04\/prefix.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1323\" \/>\n\t<meta property=\"og:image:height\" content=\"580\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Lyanna\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u00c9crit par\" \/>\n\t<meta name=\"twitter:data1\" content=\"Lyanna\" \/>\n\t<meta name=\"twitter:label2\" content=\"Dur\u00e9e de lecture estim\u00e9e\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/\"},\"author\":{\"name\":\"Lyanna\",\"@id\":\"https:\/\/magali-colas.fr\/astuces-wordpress\/#\/schema\/person\/f7cb41098a46b2339b7bf590e8aa516a\"},\"headline\":\"Automatically redirect posts with a custom prefix\",\"datePublished\":\"2023-04-18T11:29:42+00:00\",\"dateModified\":\"2023-04-18T11:29:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/\"},\"wordCount\":86,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/magali-colas.fr\/astuces-wordpress\/wp-content\/uploads\/2023\/04\/prefix.png\",\"keywords\":[\"Hook\"],\"articleSection\":[\"PHP\"],\"inLanguage\":\"fr-FR\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/\",\"url\":\"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/\",\"name\":\"Automatically 301 posts with prefix in custom structure permalinks\",\"isPartOf\":{\"@id\":\"https:\/\/magali-colas.fr\/astuces-wordpress\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/magali-colas.fr\/astuces-wordpress\/wp-content\/uploads\/2023\/04\/prefix.png\",\"datePublished\":\"2023-04-18T11:29:42+00:00\",\"dateModified\":\"2023-04-18T11:29:43+00:00\",\"author\":{\"@id\":\"https:\/\/magali-colas.fr\/astuces-wordpress\/#\/schema\/person\/f7cb41098a46b2339b7bf590e8aa516a\"},\"breadcrumb\":{\"@id\":\"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/#breadcrumb\"},\"inLanguage\":\"fr-FR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"fr-FR\",\"@id\":\"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/#primaryimage\",\"url\":\"https:\/\/magali-colas.fr\/astuces-wordpress\/wp-content\/uploads\/2023\/04\/prefix.png\",\"contentUrl\":\"https:\/\/magali-colas.fr\/astuces-wordpress\/wp-content\/uploads\/2023\/04\/prefix.png\",\"width\":1323,\"height\":580},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud83c\udfe0 Accueil\",\"item\":\"https:\/\/magali-colas.fr\/astuces-wordpress\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Hook\",\"item\":\"https:\/\/magali-colas.fr\/astuces-wordpress\/tag\/hook\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Automatically redirect posts with a custom prefix\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/magali-colas.fr\/astuces-wordpress\/#website\",\"url\":\"https:\/\/magali-colas.fr\/astuces-wordpress\/\",\"name\":\"\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/magali-colas.fr\/astuces-wordpress\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"fr-FR\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/magali-colas.fr\/astuces-wordpress\/#\/schema\/person\/f7cb41098a46b2339b7bf590e8aa516a\",\"name\":\"Lyanna\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"fr-FR\",\"@id\":\"https:\/\/magali-colas.fr\/astuces-wordpress\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/4e361b30ec79a25d91a54b45254a5fd94f2c88bcf72f01c85a103a89bbec68f4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/4e361b30ec79a25d91a54b45254a5fd94f2c88bcf72f01c85a103a89bbec68f4?s=96&d=mm&r=g\",\"caption\":\"Lyanna\"},\"sameAs\":[\"https:\/\/astuces-wp.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Automatically 301 posts with prefix in custom structure permalinks","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/","og_locale":"fr_FR","og_type":"article","og_title":"Automatically 301 posts with prefix in custom structure permalinks","og_description":"\u00a0 In the example we need all of our articles to have the following schema: Site URL news post name But once the structure is changed, not all URLs are 404. Hook template_redirect The objective of the hook is to detect when a URL responds in 404, if this is the case we look for [&hellip;]","og_url":"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/","article_published_time":"2023-04-18T11:29:42+00:00","article_modified_time":"2023-04-18T11:29:43+00:00","og_image":[{"width":1323,"height":580,"url":"https:\/\/magali-colas.fr\/astuces-wordpress\/wp-content\/uploads\/2023\/04\/prefix.png","type":"image\/png"}],"author":"Lyanna","twitter_card":"summary_large_image","twitter_misc":{"\u00c9crit par":"Lyanna","Dur\u00e9e de lecture estim\u00e9e":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/#article","isPartOf":{"@id":"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/"},"author":{"name":"Lyanna","@id":"https:\/\/magali-colas.fr\/astuces-wordpress\/#\/schema\/person\/f7cb41098a46b2339b7bf590e8aa516a"},"headline":"Automatically redirect posts with a custom prefix","datePublished":"2023-04-18T11:29:42+00:00","dateModified":"2023-04-18T11:29:43+00:00","mainEntityOfPage":{"@id":"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/"},"wordCount":86,"commentCount":0,"image":{"@id":"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/#primaryimage"},"thumbnailUrl":"https:\/\/magali-colas.fr\/astuces-wordpress\/wp-content\/uploads\/2023\/04\/prefix.png","keywords":["Hook"],"articleSection":["PHP"],"inLanguage":"fr-FR","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/","url":"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/","name":"Automatically 301 posts with prefix in custom structure permalinks","isPartOf":{"@id":"https:\/\/magali-colas.fr\/astuces-wordpress\/#website"},"primaryImageOfPage":{"@id":"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/#primaryimage"},"image":{"@id":"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/#primaryimage"},"thumbnailUrl":"https:\/\/magali-colas.fr\/astuces-wordpress\/wp-content\/uploads\/2023\/04\/prefix.png","datePublished":"2023-04-18T11:29:42+00:00","dateModified":"2023-04-18T11:29:43+00:00","author":{"@id":"https:\/\/magali-colas.fr\/astuces-wordpress\/#\/schema\/person\/f7cb41098a46b2339b7bf590e8aa516a"},"breadcrumb":{"@id":"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/#breadcrumb"},"inLanguage":"fr-FR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/"]}]},{"@type":"ImageObject","inLanguage":"fr-FR","@id":"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/#primaryimage","url":"https:\/\/magali-colas.fr\/astuces-wordpress\/wp-content\/uploads\/2023\/04\/prefix.png","contentUrl":"https:\/\/magali-colas.fr\/astuces-wordpress\/wp-content\/uploads\/2023\/04\/prefix.png","width":1323,"height":580},{"@type":"BreadcrumbList","@id":"https:\/\/magali-colas.fr\/astuces-wordpress\/redirect-prefix-wordpress\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud83c\udfe0 Accueil","item":"https:\/\/magali-colas.fr\/astuces-wordpress\/"},{"@type":"ListItem","position":2,"name":"Hook","item":"https:\/\/magali-colas.fr\/astuces-wordpress\/tag\/hook\/"},{"@type":"ListItem","position":3,"name":"Automatically redirect posts with a custom prefix"}]},{"@type":"WebSite","@id":"https:\/\/magali-colas.fr\/astuces-wordpress\/#website","url":"https:\/\/magali-colas.fr\/astuces-wordpress\/","name":"","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/magali-colas.fr\/astuces-wordpress\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"fr-FR"},{"@type":"Person","@id":"https:\/\/magali-colas.fr\/astuces-wordpress\/#\/schema\/person\/f7cb41098a46b2339b7bf590e8aa516a","name":"Lyanna","image":{"@type":"ImageObject","inLanguage":"fr-FR","@id":"https:\/\/magali-colas.fr\/astuces-wordpress\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/4e361b30ec79a25d91a54b45254a5fd94f2c88bcf72f01c85a103a89bbec68f4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4e361b30ec79a25d91a54b45254a5fd94f2c88bcf72f01c85a103a89bbec68f4?s=96&d=mm&r=g","caption":"Lyanna"},"sameAs":["https:\/\/astuces-wp.com"]}]}},"_links":{"self":[{"href":"https:\/\/magali-colas.fr\/astuces-wordpress\/wp-json\/wp\/v2\/posts\/589","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/magali-colas.fr\/astuces-wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/magali-colas.fr\/astuces-wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/magali-colas.fr\/astuces-wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/magali-colas.fr\/astuces-wordpress\/wp-json\/wp\/v2\/comments?post=589"}],"version-history":[{"count":1,"href":"https:\/\/magali-colas.fr\/astuces-wordpress\/wp-json\/wp\/v2\/posts\/589\/revisions"}],"predecessor-version":[{"id":590,"href":"https:\/\/magali-colas.fr\/astuces-wordpress\/wp-json\/wp\/v2\/posts\/589\/revisions\/590"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/magali-colas.fr\/astuces-wordpress\/wp-json\/wp\/v2\/media\/576"}],"wp:attachment":[{"href":"https:\/\/magali-colas.fr\/astuces-wordpress\/wp-json\/wp\/v2\/media?parent=589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/magali-colas.fr\/astuces-wordpress\/wp-json\/wp\/v2\/categories?post=589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/magali-colas.fr\/astuces-wordpress\/wp-json\/wp\/v2\/tags?post=589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}