1

It's possible via .htaccess send query to another file? A bit explaining:

index.php handle all pages like this:

switch($page) {
    case 'index':
        require_once START . '/includes/pages/index.php';
    break;

    case 'another':
        require_once START . '/includes/pages/another.php';
    break;
       and so on...

In .htaccess:

RewriteRule ^index$ index.php?page=index [L]
RewriteRule ^another$ index.php?page=another [L]

Links(eg. localhost/84ss7d41):

RewriteRule ^(.*)$ index.php?QUERY_STRING=$1 [L]

So in /includes/pages/index.php:

if ($_GET['QUERY_STRING']) {
$_SERVER['QUERY_STRING'] = $_GET['QUERY_STRING'];
$query_string = check_input($_SERVER['QUERY_STRING']);
    //checking/validating processing further...

How to make links like this one localhost/84ss7d41 work in same way as now, but without index.php ? I'm trying to seperate my "project" if something would happen (files accidentally deleted) checking/validating data would work same, without main script, it's like:

if (main project file index.php/ || /includes/pages/index.php doesn't exist) {
  check/validate data with reserve.php
}
ZeroSuf3r
  • 1,961
  • 8
  • 25
  • 36
  • Do not overwrite members of the `$_SERVER` array if you want to stay out of trouble later. What you try to achieve should be possible w/o but you must make more clear what you try to achieve exactly. – hakre Mar 11 '12 at 16:23
  • Can you clarify your question? You want to create links like `localhost/84ss7d41` or you want to redirect user whenever the index.php does not exists? What do you want? – MahanGM Mar 11 '12 at 19:02
  • @MahanGM exactly, whenever the index.php does not exists – ZeroSuf3r Mar 12 '12 at 11:30
  • @ZeroSuf3r, There should be at least one `index` page to handle queries. I don't know what's your application architecture but I think the standard way is to have one major page in your project. – MahanGM Mar 13 '12 at 08:32

1 Answers1

1
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule ^(.*) /index.php [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) /reserve.php [L]

This redirects all requests that do not match an existing file (for example, an image) to index.php, where you can then use $_SERVER["REQUEST_URI"] to decide how to act. This is an implementation of the Front Controller pattern http://en.wikipedia.org/wiki/Front_Controller_pattern

Additionally, if index.php cannot be found by apache, then the request will be forwarded to reserve.php

Although, building in this kind of redundancy is not necessarily the 'right' thing to do (what if reserve.php goes missing too?). Enforcing proper access controls over index.php is probably a better approach.

James Holwell
  • 938
  • 7
  • 10
  • Which would obviously lead to one "index.php" but here called "controller.php"? – steveoh Mar 15 '12 at 23:17
  • In fact it is the question I'm not gettin' here. Why would one kill the main-script? Besides that: I like the front controller pattern :) – steveoh Mar 15 '12 at 23:29