繁体   English   中英

如何在Wordpress中即时重定向URL?

[英]How to instant redirect url in wordpress?

我想在wordpress中立即重定向URL。 这是我的脚本代码。

JS代码

jQuery(document).ready(function() {
    var x=window.location.href;

    if(x=='http://example.com/') {
        window.location.href='http://example.com/home';
    }
});

当我输入网址example.com/时,我希望它可以立即重定向到example.com/home,而无需显示example.com/页面。 当我在网站上按Enter键时,我想查看example.com/home页面。

最好在htaccess文件中创建重定向,如下所示:

#Redirect a single page:
Redirect 301 /pagename.php http://www.domain.com/pagename.html

您可以使用RedirectMatch进行重定向

.htaccess文件

RedirectMatch ^/$ /home/

WordPress提供了一个易于使用的重定向功能。 只需将其放在模板的顶部即可:

<?php
wp_redirect( 'http://example.com/home' );
exit;
?>

https://codex.wordpress.org/Function_Reference/wp_redirect

使用Wordpress框架本身执行此操作的一种方法是使用wp_redirect- https: //codex.wordpress.org/Function_Reference/wp_redirect

因此,您可以加入wordpress的wp操作(以确保已设置$ post项目并运行以下命令。它将检查这是否是在“阅读设置”中设置的站点的主页,如果是,则将其重定向。

这不是解决此问题的最佳方法,因为它确实加载了某些页面,但对于此处的问题,这可能是一种快速的临时解决方案。 只需将其添加到您的functions.php文件中

add_action('init','redirect_home');
function redirect_home(){
    if(is_front_page()||is_home()){
        wp_redirect(get_site_url().'/home');exit;
    }
}

使用以下内容编辑您的htaccess文件:

RewriteEngine On
RewriteRule ^$ /home [R=301,L]
RewriteCond %{REQUEST_URI} ^/?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM