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
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 :<
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?
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
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!
<% 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.
But in my case window.location.href
wasn't doing the trick :(