+ Reply to Thread
Results 1 to 7 of 7

Thread: javascript redirect to main page if subpage called directly

  1. #1
    slacker3 is offline x10 Sophmore slacker3 is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    146

    Question javascript redirect to main page if subpage called directly

    Ok this may sound like an stupid question but i can't figure out how to reliably do an javascript redirect if (and only if) an subpage is called directly.

    My website generates dynamic content so i created an sitemap to make everything accessible for search engines. Now if someone clicks on an search-engine hit, for example

    example.com/getPage.php?page=14

    an ugly (and invalid) html page shows off, since it's never meant to be viewed directly.


    Is there any way to reliably redirect visitors to the main page? Checking for $HTTP_REFERER isn't the best solution, i guess.

  2. #2
    marshian's Avatar
    marshian is offline x10 Elder marshian is an unknown quantity at this point
    Join Date
    Jan 2008
    Location
    Belgium
    Posts
    526

    Re: javascript redirect to main page if subpage called directly

    You can use
    PHP Code:
    header("Location: http://yourdomain.com/main.php"); 
    to redirect people from one page to another.
    If you only want that to happen when $_GET["page"] is set, do this:
    PHP Code:
    if(isset($_GET["page"])) {
     
    header("Location: http://yourdomain.com/main.php");

    I wouldn't say any JavaScript redirect is reliable, but if you need to, you can easily do something like this (untested):
    PHP Code:
    <?php
    //loads of stuff here
    if(isset($_GET["page"])) {
    ?>
    <script type="text/javascript">
    window.location = "http://yoursite.com/main.php";
    </script>
    <?php
    }
    //more stuff here
    ?>
    Does that help you?
    Real programmers don't document their code - if it was hard to write, it should be hard to understand.

  3. #3
    slacker3 is offline x10 Sophmore slacker3 is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    146

    Smile Re: javascript redirect to main page if subpage called directly

    That's fine - thanks!

    sent you 50 x10hosting-bucks..




    now i will use
    "getPage.php?page=14&viewedFromMainPage=1" internally

    and in getPage.php:
    PHP Code:
    if(isset($_GET["viewedFromMainPage"])) {
    // display my stuff
    }
    else 
    redirect(); 
    the sitemap just shows "http://example.com/getPage.php?page=14"


    EDIT:
    redirecting with javascript, otherwise google won't index the page (almost forgot about that..)
    PHP Code:
    if(!isset($_GET["viewedFromMainPage"])) {
    // javascript redirect
    }

    // html content here 
    Last edited by slacker3; 01-07-2010 at 05:52 AM.

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: javascript redirect to main page if subpage called directly

    Out of curiosity, how are you integrating the subpage content into top-level pages? include?
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  5. #5
    slacker3 is offline x10 Sophmore slacker3 is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    146

    Re: javascript redirect to main page if subpage called directly

    Quote Originally Posted by misson View Post
    Out of curiosity, how are you integrating the subpage content into top-level pages? include?

    No, otherwise i could just have set an global variable to check if it's called from within index.php (i believe).

    At first i really wanted to do it C-style, something like
    #define CALLEDFROMMAIN // in main .php

    #ifndef CALLEDFROMMAIN // in getpage.php
    // redirect stuff

    (which was stupid.. )



    index.php:
    HTML Code:
    <div class="article" id="article">
    <div id="pageContainer">
    </div> 
    </div>
    
    <script type="text/javascript">
    var dynPageObj = new DHTMLgoodies_scrollingPages();
    dynPageObj.setTargetId('pageContainer');
    dynPageObj.setUrl('getPage.php?pageNo=0');
    dynPageObj.setScrollSpeed(10);
    dynPageObj.loadPage();
    </script>
    getpage.php
    PHP Code:
    <?php
    if(!isset($_GET['IwasCalledFromMain'])){
      print 
    '<script type="text/javascript">';
      print 
    '// redirect me..';
      print 
    '</script>';
    }

    if(isset(
    $_GET['pageNo'])){
        
    // just insert right after intro.html
        
    $articles = array("intro.html""undelete.html""ftp.html"); 
        
    $numofarticles count($articles)-1;
               
        if(
    $_GET['pageNo']>=&& $_GET['pageNo'] <= $numofarticles) {         
            include(
    'articles/' $articles[$_GET['pageNo']]);
            if(
    $_GET['pageNo'] > 0)
              print (
    'go to <a href="#">Top</a> '); 
        }

        if(
    $_GET['pageNo']<$numofarticles){
        echo 
    "<a href=\"#\" onclick=\"dynPageObj.setUrl('getPage.php?pageNo=".($_GET['pageNo']+1)."');dynPageObj.loadPage();this.style.display='none';return false\">Next page</a> ";
        }
    }
    ?>
    but have a look on it yourself if you want
    ugly invalid html: http://j05t.co.cc/getPage.php?pageNo=1
    main page: http://j05t.co.cc/ (just click on the penguin logo)


    (still working on it.. sometimes ;) )
    Last edited by slacker3; 01-07-2010 at 08:52 AM.

  6. #6
    marshian's Avatar
    marshian is offline x10 Elder marshian is an unknown quantity at this point
    Join Date
    Jan 2008
    Location
    Belgium
    Posts
    526

    Re: javascript redirect to main page if subpage called directly

    If you only need the JS redirect to get search engines to index those pages, it might be a good idea to make those pages valid as well. Search engines usually aren't very smart and just follow the html tree in order to index a page.
    My suggestion would be, if !isset($_GET['IwasCalledFromMain'] to add more than just the script. Complete the page, add an html element, head element, etc. There's no reason to make the style look great though, so basic style would be more than sufficient. You can add a link to "go to the main page" too.
    Why is this? Search engines will find it easier to index your site: better hits, more hits, etc. Secondly, if a search engine indexes that page, it won't give the user a link to the main page, but to the "crappy" one. So it's not unlikely a user ends up on that page instead of on the main page.

    And thanks for the credits
    Real programmers don't document their code - if it was hard to write, it should be hard to understand.

  7. #7
    slacker3 is offline x10 Sophmore slacker3 is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    146

    Re: javascript redirect to main page if subpage called directly

    you're right..

    If anyone (or some crawler) decides to view the "crappy" version i will include an javascript redirect as well as an backlink to index.php and the most basic stuff needed to make it XHTML-valid.

    Otherwise it will just be loaded fine into the <div>.

    win/win..

+ Reply to Thread

Similar Threads

  1. Main page / Index.html not showing
    By mkstalal in forum Free Hosting
    Replies: 1
    Last Post: 11-14-2007, 02:04 PM
  2. Replies: 1
    Last Post: 09-26-2007, 08:51 AM
  3. Scripts taking users to main domain page
    By zero5854 in forum Free Hosting
    Replies: 31
    Last Post: 08-07-2007, 12:29 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers