Redirecting to another site was very easy in PHP. If they wanted to redirect visitors from "www.yoursite.com/news" to "www.bbc.com"...all I had to do was make the "news" folder and create an index.php file in it and add this line:
在PHP中,重定向到另一个站点非常容易。如果他们想将访问者从“www.yoursite.com/news”重定向到“www.bbc.com”......我所要做的就是制作“news”文件夹并在其中创建一个index.php文件并添加这一行:
<?php
header("Location: http://www.bbc.com/");
?>
And I was done! But I recently started working on a project written in Ruby, and having trouble to figure out how to achieve this simple task :<
我完成了!但是我最近开始研究用Ruby编写的项目,并且无法弄清楚如何实现这个简单的任务:<
This might be a very silly question, but any help will be greatly appreciated!!
这可能是一个非常愚蠢的问题,但任何帮助将不胜感激!
UPDATE:
更新:
So found out that this project is using static site generator Middleman to build the site, that's why there is no routes.rb
file. It only has config.rb
. Can anyone please help me to figure out how to redirect in middleman?
所以发现这个项目是使用静态站点生成器Middleman来构建站点,这就是为什么没有routes.rb文件。它只有config.rb。任何人都可以帮我弄清楚如何在中间人重定向?
2nd UPDATE:
第二次更新:
Looks like because of Middleman, this redirecting isn't possible that simply. So I am asking this (very dumb) question: How to redirect URL in Javascript or jQuery
看起来像是因为Middleman,这种重定向是不可能的。所以我问这个(非常愚蠢)的问题:如何在Javascript或jQuery中重定向URL
2 个解决方案
#1
2
So finally found an easy solution to my problem :)
所以终于找到了解决我问题的简单方法:)
In order to redirect www.yoursite.com/news to www.bbc.com, first I created a news.html.erb
file under my source
folder (where you have your index.html.erb
file). And in that file I added the following lines and wallaaahhhh!
为了将www.yoursite.com/news重定向到www.bbc.com,我首先在源文件夹下创建了一个news.html.erb文件(你有index.html.erb文件)。在该文件中,我添加了以下行和wallaaahhhh!
<% content_for :body_content do %>
<script type="text/javascript">
// Javascript URL redirection
window.location.replace("http://www.bbc.com/");
</script>
<noscript>
// Using HTML refresh meta tag as a fail back in case the user has javascript disabled
<meta http-equiv="refresh" content="0;URL=http://www.bbc.com/">
</noscript>
<% end %>
One thing I should mention here is that most sites suggested
我应该在这里提到的一件事是,大多数网站建议
using
window.location.href
, becausereplace()
does not put the originating page in the session history, meaning the user won’t get stuck in a never-ending back-button process.使用window.location.href,因为replace()不会将原始页面放在会话历史记录中,这意味着用户不会陷入永无止境的后退按钮过程。
But in my case window.location.href
wasn't doing the trick :(
但在我的情况下window.location.href没有做的伎俩:(
#2
1
You'll need to setup a custom route in routes.rb:
您需要在routes.rb中设置自定义路由:
get "/news" => redirect("http://bbc.com")
#1
2
So finally found an easy solution to my problem :)
所以终于找到了解决我问题的简单方法:)
In order to redirect www.yoursite.com/news to www.bbc.com, first I created a news.html.erb
file under my source
folder (where you have your index.html.erb
file). And in that file I added the following lines and wallaaahhhh!
为了将www.yoursite.com/news重定向到www.bbc.com,我首先在源文件夹下创建了一个news.html.erb文件(你有index.html.erb文件)。在该文件中,我添加了以下行和wallaaahhhh!
<% content_for :body_content do %>
<script type="text/javascript">
// Javascript URL redirection
window.location.replace("http://www.bbc.com/");
</script>
<noscript>
// Using HTML refresh meta tag as a fail back in case the user has javascript disabled
<meta http-equiv="refresh" content="0;URL=http://www.bbc.com/">
</noscript>
<% end %>
One thing I should mention here is that most sites suggested
我应该在这里提到的一件事是,大多数网站建议
using
window.location.href
, becausereplace()
does not put the originating page in the session history, meaning the user won’t get stuck in a never-ending back-button process.使用window.location.href,因为replace()不会将原始页面放在会话历史记录中,这意味着用户不会陷入永无止境的后退按钮过程。
But in my case window.location.href
wasn't doing the trick :(
但在我的情况下window.location.href没有做的伎俩:(
#2
1
You'll need to setup a custom route in routes.rb:
您需要在routes.rb中设置自定义路由:
get "/news" => redirect("http://bbc.com")