0

I was wondering if it was possible to force users to access my website from the homepage only.

For example, if the user enters the address mysite.com/subdirectory (which a valid directory) I want them to be redirected to mysite.com/index.html . From there the can navigate to /subdirectory with the hyperlinks on the homepage.

Thanks in advance!

stojda
  • 61
  • 6
  • 5
    Why potentially annoy your users like this? What if they want to bookmark "subdirectory" so they can go there quickly, say? Chances are there's a better way of achieving what you actually want, but we can't really comment unless we know why you want to do this thing, which is, in some ways, "breaking the web". – Matt Gibson Apr 19 '14 at 14:13
  • Fair enough. Essentially the majority of my site is hosting resource files for one of my classes. Originally i was just going to make hyperlinks to each file from a single page, but there are so many files I don't really want to do that, so i'm using the default Apache directory indexer (I think that's what its called) to allow users to navigate to the files. I wanted to force them through the homepage to make them see the adverts and potentially generate revenue to pay for the hosting. – stojda Apr 19 '14 at 14:18
  • 1
    I personally think, you should replan this, It sounds weird to me and it's totally "un-web" – toesslab Apr 19 '14 at 14:21
  • You can check the referrer header, but this could be disabled or spoofed. Or you can have your homepage links include a nonce that is verified when the sub-page loads. – Anthony Apr 19 '14 at 14:21
  • 1
    So you are too lazy too write an HTML page but also too broke to cover your hosting overhead? And you want to put in time and effort to hopefully make 10 cents for ads loading? – Anthony Apr 19 '14 at 14:24
  • This won't work since every solution requires actually writing an actual HTML page with php on the back end. Like a real web page. – Anthony Apr 19 '14 at 14:28
  • Given that this question is tagged PHP, it wouldn't be hard to write a php script that iterated all files in the same directory and created a bunch of download links for them, wrapped in advertising. That's certainly a better—and more reliable—solution in my mind than redirecting everyone to the homepage. – Matt Gibson Apr 19 '14 at 14:29
  • @MattGibson That seems like a better idea, should have thought of that. Can you post an answer on how to do that please? thanks :) – stojda Apr 20 '14 at 14:01

4 Answers4

0

You could use a $_SESSION variable defined in the homepage and check in other parts of the website if it is defined. If it's not redirect to your homepage.

For example, your homepage could be:

session_start();

if (!isset($_SESSION['homepage']){
    $_SESSION['homepage'] = true;
}

// Your homepage code

and any other page should start with:

session_start():

if(!isset($_SESSION['homepage'] || $_SESSION['homepage'] != true){
   header("Location: index.php");
}

Anyway, I can't understand why you would want to do this and I think it should be a proper way of doing what you desire without forcing the user to do this.

barbarity
  • 2,420
  • 1
  • 21
  • 29
  • That would only force them to hit the homepage and start the session and then go directly to other pages from there. – Anthony Apr 19 '14 at 14:19
  • @Anthony, isn't that what the OP wants? – barbarity Apr 19 '14 at 14:22
  • No, he wants them to hit the homepage everytime a sub-page is accessed. With a session variable, I could go to it once and then use bookmarks every other time. – Anthony Apr 19 '14 at 14:27
  • Well, he could destroy the session every time a user accesses a subdirectory, but it seems so wrong for the user experience that i totally not recommend to do that. – barbarity Apr 19 '14 at 14:29
  • Although using the session will generally work, it will break the site for users with cookies disabled. – Boann Apr 19 '14 at 14:45
0

One method is to require a POST request to access each subpage. If the user accesses the page directly, it will be a GET request instead. On your home page, do something like this for each link:

<form method=post action="/subdirectory">
<input type=submit value="My stuff">
</form>

(You can use client-side JavaScript to re-work those buttons into links which activate the forms, if desired.)

Then on each subpage:

<?php
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
    header('Location: /');
    die();
}
Boann
  • 48,794
  • 16
  • 117
  • 146
0

As all you want to do is to ensure your users see some advertising, and you're currently using the standard Apache directory listing, I'd simply substitute a PHP script (as index.php in each subdirectory) that lists the files in a similar way, but inside an HTML page that you can customise with your advertising. Something along these lines, perhaps, where I've adapted this answer and wrapped it in a web page:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Download Page Example</title>
</head>
<body>
<h1>Download Page Example</h1>
<p>Here's some advertising.</p>
<ul class="file-list">
<?php
  // Iterate all files in this directory
  $dir = new DirectoryIterator(dirname(__FILE__));
  foreach ($dir as $fileinfo) {
    $filename = $fileinfo->getFilename();
    // If it's not a hidden "dot file"
    if (!fnmatch('.*', $filename)) {
      // And put it on the list as a link, encoding the
      // filename as a url path safely in the achor attribute
      // and as HTML in the text.
      echo '<li><a href="' . rawurlencode($filename) . '">' . htmlentities($filename) . '</a></li>';
    }
  }
?>
</ul>
</body>
</html>
Community
  • 1
  • 1
Matt Gibson
  • 37,886
  • 9
  • 99
  • 128
-2

you should be able to do this using apache's mod_rewrite

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

put this in an .htaccess file in your main directory

k961
  • 577
  • 1
  • 5
  • 19