javascript redirect to main page if subpage called directly

slacker3

New Member
Messages
146
Reaction score
6
Points
0
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.
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
You can use
PHP:
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:
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:
<?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?
 

slacker3

New Member
Messages
146
Reaction score
6
Points
0
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:
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:
if(!isset($_GET["viewedFromMainPage"])) {
// javascript redirect
}

// html content here
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Out of curiosity, how are you integrating the subpage content into top-level pages? include?
 

slacker3

New Member
Messages
146
Reaction score
6
Points
0
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.. :biggrin:)



index.php:
HTML:
<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:
<?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']>=0 && $_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:

marshian

New Member
Messages
526
Reaction score
9
Points
0
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 :)
 

slacker3

New Member
Messages
146
Reaction score
6
Points
0
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.. :biggrin:
 
Top