如何通过 .htaccess 将 Wordpress 404 指向 URL
How to point Wordpress 404 to URL through .htaccess
我在实际页面中创建了一个 404 页面。
ErrorDocument 404 /404.html
上面的代码用于重定向 404,但这是指向文件而不是 URL?
如何将 404 重定向到 CMS 中的页面?
我尝试了以下方法,但无济于事:
ErrorDocument 404 https://www.mywebsite.com/my-404-page
非常感谢
试试这个答案:
据我所知,.htaccess 重写规则是在请求被 WordPress 处理之前执行的,甚至是 PHP 引擎,所以你不知道 URL 是否会触发 404 .htaccess 中的错误,您无法检查给定路径是否会在该级别触发 404。您需要在 WordPress 本身中执行此操作:
// See https://developer.wordpress.org/reference/hooks/template_redirect/
add_action( 'template_redirect', 'cyb_redirect_not_found_paths' );
function cyb_redirect_not_found_paths() {
// See https://developer.wordpress.org/reference/functions/is_404/
if( is_404() ) {
if( $_SERVER['REQUEST_URI'] == '/your_path' ) {
$url_to = 'redirecton_rul';
// See https://developer.wordpress.org/reference/functions/wp_redirect/
wp_redirect( $url_to );
exit;
}
}
}
要使用您建议的方法执行此操作,您需要包含重写规则:
RewriteRule ^404/?$ my-404-page.html
所以加起来就是:
RewriteRule ^404/?$ my-404-page.html
ErrorDocument 404 https://www.mywebsite.com/my-404-page/
确保在测试之前清除缓存。
我在实际页面中创建了一个 404 页面。
ErrorDocument 404 /404.html
上面的代码用于重定向 404,但这是指向文件而不是 URL?
如何将 404 重定向到 CMS 中的页面?
我尝试了以下方法,但无济于事:
ErrorDocument 404 https://www.mywebsite.com/my-404-page
非常感谢
试试这个答案:
据我所知,.htaccess 重写规则是在请求被 WordPress 处理之前执行的,甚至是 PHP 引擎,所以你不知道 URL 是否会触发 404 .htaccess 中的错误,您无法检查给定路径是否会在该级别触发 404。您需要在 WordPress 本身中执行此操作:
// See https://developer.wordpress.org/reference/hooks/template_redirect/
add_action( 'template_redirect', 'cyb_redirect_not_found_paths' );
function cyb_redirect_not_found_paths() {
// See https://developer.wordpress.org/reference/functions/is_404/
if( is_404() ) {
if( $_SERVER['REQUEST_URI'] == '/your_path' ) {
$url_to = 'redirecton_rul';
// See https://developer.wordpress.org/reference/functions/wp_redirect/
wp_redirect( $url_to );
exit;
}
}
}
要使用您建议的方法执行此操作,您需要包含重写规则:
RewriteRule ^404/?$ my-404-page.html
所以加起来就是:
RewriteRule ^404/?$ my-404-page.html
ErrorDocument 404 https://www.mywebsite.com/my-404-page/
确保在测试之前清除缓存。