Jump to content
xisto Community

Redirecting Visitors To Different Page Explains two methods.

Recommended Posts

Well, almost all of you would have experienced redirecting pages while you were downloading something on the net. Redirecting users to new pages is a fact of life when programming dynamic websites. The primary reason is that you often need to redirect a user after making session state changes. For example, when you add an item to a shopping cart, you want the user to go to a page such as the current shopping cart, and you don't want to add another one of the same item to the cart again if you reload that shopping cart page. The same case if you developed guestbook where page refresh can lead to multiple posts. And in other case if a user came from search engine and the page was moved you still want that page to be accessible. Most sites achieve this with a script that processes the state change and then redirects to the page that they want the user to see.


There are essentially two ways to do it:


First Method: (preferred and best way)


This method uses the HTTP Location header, like this:



header("Location: new_page.php");


The header() function sends raw HTTP headers to the user's browser. Therefore, you need to do this before sending any other output to the browser, just as you would when setting a cookie. There are two advantages to this method. First, it is instant, and the intermediate script does not appear in a web browser's history. Second, because it is HTTP based, it does not require a web browser to process; automatic web crawlers and programs such as wget understand it.


Second Method:


However, if you would like to show an intermediate page to the user with a delay, you need to use a different method that uses the HTML <meta> tag. It's pretty simple; to send a user to a different page after displaying the current page for five seconds, put the following in your HTML header:


<meta http-equiv="Refresh" content="5;URL= new_page.php" />

All web browsers understand this, but automatic page crawlers sometimes do not. In addition, this intermediate page will show up in the user's browser history. Beware, page refresh in meta tags may be considered spam by search kings like google and yahoo.



Redirection's use when shifting domains:


Ok, so you were using a free subdomain like .co.nr or others. You have developed your site well, attracted some good traffic, and even probably earned some money through advertisements. You are now thinking to buy a cool .com domain. Yes! you even masked your .co.nr address to just show the same link in every page so that when you change domain your old links just dont throw page not found error on visitors face. But wait! What about the traffic which you would be recieving on the old address. If you were using a .co.nr address you had to show thier ad link on the first page, but you have your own domain right? So, the first idea of directly updating your .co.nr address to the new domain is not nice at all.


So you already know that you can use a page in between containing "This site is being redirected to new address" and redirect after five seconds. But .co.nr people are smart and wont allow you to do this neither direct redirecting using php method. In addition to bots they manually check the users subdomains. So what you can do???


Simple. Just create an exact replica of your main index page and add the ad link which they want you to show. Change every link on that page with thier respective address in your new domain. Now link this page to your old .co.nr address. That's it. Now your visitors coming from new domain won't be seeing any .co.nr ad link. And in the process also taking care of the visitors from the old address. Also you can host this page on your current host and can even add a statistic logging code to this page to see how many people from which place are still coming through your old address.


These things would not have been possible if you directly linked to your new address. Was it? I have seen majority (even myself) of people using .co.nr domains and this may be the only method you would use for this major change.

Share this post

Link to post
Share on other sites

There is also another way of doing this. through the used of javascripts.

<scirpt>setTimeout("foo()",5);function foo(){window.href = "otherpage.html";}</script>

this will actually work just like have the meta tag but search engines would not flag your web page as a spam. If you don't want to ensure that there is not output before calling the function (i.e. header function) then you can do this by invoking the function ob_start() which buffers the page in the server before sending it to the clients browser. so you can rest assure that your header will still work even if there is already an output sent.

Share this post

Link to post
Share on other sites

Page redirection in various coomon web development languages,


java script:

<script type="text/javascript">   window.location = "google.com/&%2334;</script>
If you want a time delay use the following code,


<script type="text/javascript">	function handleRedirect() {		setTimeout(redirectTo("/, 5000); //Redirect after 5 Seconds	}	function redirectTo(url) {		window.location = url;	}	window.load = handleRedirect();</script>


The 301 Redirect is used to point to new page or new website the Search Engine Friendly way. The 301 redirect is interpreted as "Permanently Moved".


Here is how we do 301 redirect in PHP:


<?   Header( "HTTP/1.1 301 Moved Permanently" );    Header( "Location: http://NEW_DOMAIN/" ); ?>
Make sure that you put this code in the first line of the page.


301 redirect using Java Server Pages (JSP):


<%   response.setStatus(301);   response.setHeader( "Location", "http://NEW_DOMAIN/" );   response.setHeader( "Connection", "close" );%>
301 redirect using Ruby on Rails:

def ACTION_NAME   headers["Status"] = "301 Moved Permanently"   redirect_to "http://NEW_DOMAIN/"end
Replace the ACTION_NAME with the action that you want to handle the redirection.


301 redirect using .htaccess.


Create a .htaccess file in the root directory of your website and put the following in it,

Options +FollowSymLinksRewriteEngine onRewriteRule (.*) http://NEW_DOMIAN/$1 [R=301,L]
Replace NEW_DOMAIN with your new domain or the updated page path in the above code snippets.

Share this post

Link to post
Share on other sites

How about this redirect javascript. Too short.


If your page is in a frame and you want the whole page redirects.

Edited by Erdemir (see edit history)

Share this post

Link to post
Share on other sites

I believe the best way is still this <?header("Location: new_page.php");?>No need to complicate things, this is the easiest and fuss free way to go about doing it. Cheers.

Share this post

Link to post
Share on other sites
How to redirect visitors from specific referring domains?Redirecting Visitors To Different Page

Great info here! Using .Htaccess file, how can I redirect visitors from certain referring domains to a landing page?  We advertise on Facebook, myspace, sherdog.Com and etc and would like visitors who click on our ads to be redirected to a landing page before they enter the site.  This way we can change the landing page every so often or change the htaccess to point to a new page when we have a promotion going on.

 Just curious if I can do this with htaccess by listing out the referring domains to send all visitors to landingpage.Php or whatever we call it.



-reply by Chris

Share this post

Link to post
Share on other sites

Very interesting information. Is it possible to add colors and text to the screen when this is in effect as well?I've been messing with creating custom 404's for CMS's to where it will automatically take you to the home page if you type a wrong address, but I am only able to do it on a white background w/ black text. More or less it just says "Redirecting..." or something like that.I would love to be able to change the color to look the same as the CMS... It just doesn't feel like it fits right.Also, in your method one. This is just telling the browser that instead of, say "jokes.html" you are on "index.html"? Like you load up the jokes but if you refresh it would take you to index? (Assuming you set it as "index.html" I mean.

Share this post

Link to post
Share on other sites
How browsing history page of websiteRedirecting Visitors To Different Page

How browsing history page of website. When user browse through pages of website at the end user can see which pages he browsed.[using PHP sessions]

-question by Manjunath

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.