{"id":2030,"date":"2014-09-30T13:53:51","date_gmt":"2014-09-30T13:53:51","guid":{"rendered":"http:\/\/musilda.cz\/?p=2030"},"modified":"2014-09-30T13:53:51","modified_gmt":"2014-09-30T13:53:51","slug":"presmerovani-neexistujicich-url-pomoci-301-na-homepage-webu","status":"publish","type":"post","link":"https:\/\/affinite.io\/cs\/presmerovani-neexistujicich-url-pomoci-301-na-homepage-webu\/","title":{"rendered":"P\u0159esm\u011brov\u00e1n\u00ed neexistuj\u00edc\u00edch url pomoc\u00ed 301 na homepage webu"},"content":{"rendered":"
P\u0159esto\u017ee se n\u00e1s Google sna\u017e\u00ed p\u0159esv\u011bd\u010dit o opaku, s\u00edla odkaz\u016f se ned\u00e1 pop\u0159\u00edt. A pokud odkaz vede na neexistuj\u00edc\u00ed str\u00e1nku, p\u0159ich\u00e1z\u00edte o jeho vliv na to, jak v\u00e1s vyhled\u00e1va\u010de hodnot\u00ed. Uk\u00e1\u017eeme si, jak tomu zabr\u00e1nit.<\/p>\n
Dne\u0161n\u00ed \u010dl\u00e1nek nebude dlouh\u00fd, proto\u017ee ani \u0159e\u0161en\u00ed nen\u00ed nijak n\u00e1ro\u010dn\u00e9. Bu\u010f popros\u00edte program\u00e1tora, nebo pou\u017eijete Ctrl+c a Ctrl+v a k\u00f3d, kter\u00fd v\u00e1m uk\u00e1\u017ei, vlo\u017e\u00edte do functions.php.<\/p>\n
V p\u0159\u00edpad\u011b, \u017ee odkaz na v\u00e1\u0161 web, vede na neexistuj\u00edc\u00ed str\u00e1nku, m\u00e1 WordPress vytvo\u0159en\u00fd p\u011bkn\u00fd mechanizmus, kter\u00fd v\u00e1s odk\u00e1\u017ee na vlastn\u00ed 404 str\u00e1nku. To samoz\u0159ejm\u011b, pokud m\u00e1te v \u0161ablon\u011b soubor 404.php. D\u00edky tomu m\u00e1te mo\u017enost, vytvo\u0159it si obsah, jen\u017e se u\u017eivateli zobraz\u00ed a ten pak na webu m\u016f\u017ee z\u016fstat.<\/p>\n
Bohu\u017eel neexistuj\u00edc\u00ed str\u00e1nka vrac\u00ed 404 error a s\u00edla odkazu tak p\u0159ijde vnive\u010d. Pomoc\u00ed funkce m\u016f\u017eeme takov\u00fd odkaz detekovat a p\u0159esm\u011brovat jej s 301 k\u00f3dem na hlavn\u00ed stranu.<\/p>\n
P\u0159iprav\u00edme si funkci<\/p>\n
\nfunction link_power_saver(){\n\n}\n<\/pre>\nP\u0159id\u00e1me glob\u00e1ln\u00ed prom\u011bnnou $wp_query a zjist\u00edme, zda se jedn\u00e1 o 404<\/p>\n
\nfunction link_power_saver(){\n global $wp_query;\n\tif ( $wp_query->is_404 ) {\n\n }\n}\n<\/pre>\nP\u0159ed p\u0159esm\u011brov\u00e1n\u00edm mus\u00edme je\u0161t\u011b vylou\u010dit url, kterou pou\u017e\u00edv\u00e1 Google pro verifikaci.
\nP\u0159iprav\u00edme si prom\u011bnnou $root a pokud se url neshoduje s verifika\u010dn\u00ed, p\u0159esm\u011brujeme na homepage.<\/p>\n
\nfunction link_power_saver(){\n global $wp_query;\n\tif ( $wp_query->is_404 ) {\n\n $root = '\/';\n\t\tif ( preg_match( '#^http:\/\/[^\/]+(\/.+)$#', get_option( 'siteurl' ), $matches ) ) {\n\t\t\t$root = $matches[1];\n\t\t}\n\n\t\tif ( $root[ strlen($root) - 1 ] != '\/' ) {\n\t\t\t$root .= '\/';\n\t\t}\n\n\t\tif ( strpos( $_SERVER['REQUEST_URI'], $root.'noexist_' ) !== 0 ) {\n\t\t\twp_redirect( get_bloginfo( 'siteurl' ) , 301 );\n\t\t\texit();\n\t\t}\n\n }\n}\n<\/pre>\nCelou funkci pak zav\u011bs\u00edme do akce wp<\/p>\n
\nadd_action('wp', 'link_power_saver', 100);\n<\/pre>\n