简体   繁体   English

Javascript / jQuery预先重定向?

[英]Javascript/ jquery advance redirect?

I have two websites (twigplay.com and mimuz.com). 我有两个网站(twigplay.com和mimuz.com)。 I want to redirect twigplay.com pages to the corresponding mimuz.com pages. 我想将twigplay.com页面重定向到相应的mimuz.com页面。 For example, if the user navigates to twigplay.com/about.html, I want them to be redirected to mimuz.com/about.html. 例如,如果用户导航到twigplay.com/about.html,我希望将它们重定向到mimuz.com/about.html。 I want this to be dynamic, because I have over 60,000 URLs. 我希望它是动态的,因为我有超过60,000个URL。 I have an idea on how to do that, but I don't know much about javascript. 我对如何做到这一点有一个想法,但是我对javascript不太了解。

My idea: Get current page uri > save to variable > redirect to mimuz.com/page-uri 我的想法:获取当前页面uri>保存到变量>重定向到mimuz.com/page-uri

Is this possible with javascript? 使用JavaScript可以吗?

Load this javascript in the webpage for all twigplay.com urls : 在网页中为所有twigplay.com网址加载此javascript:

$(document).ready(function() {
var pathname =$(location).attr('href');
var arr= pathname.split('/');
window.location = "mimuz.com/"+arr[arr.length-1];
});

But i strongly recommend that you use Server side scripting language (eg: php etc) for security reasons as these methods with java script are only hacks and not full proof. 但是出于安全原因,我强烈建议您使用服务器端脚本语言(例如php等),因为这些带有Java脚本的方法仅是黑客,而不是充分的证据。

var pathname = window.location.pathname; var pathname = window.location.pathname;
window.location="http://mimuz.com"+pathname; window.location =“ http://mimuz.com” +路径名;

If you use PHP, place this code at the very top of your page before HTML 如果使用PHP,请将这段代码放在HTML之前的页面顶部

<?php
$url= "http://mimuz.com/".basename($_SERVER['PHP_SELF']); /* Getting the current page */
header("Location: $url");
?>

It is possible but probably not the ideal place to do the redirect. 可能但可能不是理想的重定向位置。 If you added this javascript to all of the pages on twigplay.com it would handle the redirect for you: 如果您将此JavaScript添加到twigplay.com的所有页面上,它将为您处理重定向:

<script type="text/javascript">
  (function(){
      // changes the location's hostname, the path remains the same
      // so if you're at http//twigplay.com/whatever/the/url.html it will
      // redirect to http://mimuz.com/whatever/the/url.html
      window.location.hostname = 'mimuz.com';
  })();
</script>

It may be more ideal to setup a redirect on your server though to issue HTTP 301 which communicates the fact that you've moved the content to a new domain. 尽管发出HTTP 301来传达您已将内容移至新域这一事实,但在服务器上设置重定向可能更为理想。 If you're using an Apache webserver you would want to use mod_rewrite to issue the redirects. 如果您使用的是Apache Web服务器,则需要使用mod_rewrite发出重定向。 A sample set of rules you would put in your .htaccess file: 您将在.htaccess文件中放入的一组规则示例:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^twigplay\.com$
RewriteRule ^(.*)$ http://mimuz.com/$1 [L,R=301]

This essentially tells Apache to redirect any url that goes to twigplay.com to redirect to the same url but at mimuz.com and to issue a HTTP 301 Moved Permanently header which will inform search engines that whatever info they had about the original url should apply to the new one. 这实际上告诉Apache将重定向去twigplay.com重定向到相同的网址的,但在mimuz.com及发行永久移动头,它将通知搜索引擎,无论信息他们对原网址应该申请一个HTTP 301到新的。

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

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