PHP Classes

How To Route Pages To The Index Page And Using Htaccess

Recommend this page to a friend!

      Top level forums  >  PHP Specialists  >  General  >  How To Route Pages To The Index Page...  
Subject:How To Route Pages To The Index Page...
Summary:I want to know how to route all site's pages to the index page
Messages:2
Author:Samuel Ladapo
Date:2016-12-21 20:35:33
Update:2016-12-21 20:43:45
 

  1. How To Route Pages To The Index Page...   Reply   Report abuse  
Picture of Samuel Ladapo Samuel Ladapo - 2016-12-21 20:43:45
I have been working on this php project for a while and i have been having real issues with it,its a big project though and i want to route all my urls to the index page so as to validate each pages and create a neat clean url.
example:
foam.com/sammy [a user profile]
foam.com/sammy/pictures [sammy's picture page]
form.com/about [the site's about page]

i want everything to be the index page
not this
foam.com/user/sammy [x]
foam.com/usersammy/pictures
form.com/page/about

this is a sample code i found some where online

htacess:

RewriteEngine On
RewriteRule !\.(gif|jpg|png|css|js|html|ico|zip|rar|pdf|xml|mp4|mpg|flv|swf|mkv|ogg|avi|woff|svg|eot|ttf|jar)$ index.php [NC,L]


index page


<?php

function die404() {
header('HTTP/1.1 404 Not Found');
die(include '404.php');
}

/*
replace array indexes:
1) fix windows slashes
2) strip up-tree ../ as possible hack attempts
*/
$URL = str_replace(
array( '\\', '../' ),
array( '/', '' ),
$_SERVER['REQUEST_URI']
);

if ($offset = strpos($URL,'?')) {
// strip getData
$URL = substr($URL,0,$offset);
} else if ($offset = strpos($URL,'#')) {
/*
Since hashes are after getData, you only need
to strip hashes when there is no getData
*/
$URL = substr($URL,0,$offset);
}

/*
the path routes below aren't just handy for stripping out
the REQUEST_URI and looking to see if this is an attempt at
direct file access, they're also useful for moving uploads,
creating absolute URI's if needed, etc, etc
*/
$chop = -strlen(basename($_SERVER['SCRIPT_NAME']));
define('DOC_ROOT',substr($_SERVER['SCRIPT_FILENAME'],0,$chop));
define('URL_ROOT',substr($_SERVER['SCRIPT_NAME'],0,$chop));

// strip off the URL root from REQUEST_URI
if (URL_ROOT != '/') $URL=substr($URL,strlen(URL_ROOT));

// strip off excess slashes
$URL = trim($URL,'/');

// 404 if trying to call a real file
if (
file_exists(DOC_ROOT.'/'.$URL) &&
($_SERVER['SCRIPT_FILENAME'] != DOC_ROOT.$URL) &&
($URL != '') &&
($URL != 'index.php')
) die404();

/*
If $url is empty of default value, set action to 'default'
otherwise, explode $URL into an array
*/
$ACTION = (
($URL == '') ||
($URL == 'index.php')
) ? array('default') : explode('/',html_entity_decode($URL));
/*
I strip out non word characters from $ACTION[0] as the include
which makes sure no other oddball attempts at directory
manipulation can be done. This means your include's basename
can only contain a..z, A..Z, 0..9 and underscore!

for example, as root this would make:
pages/default.php
*/
$includeFile = 'pages/'.preg_replace('/[^\w]/','',$ACTION[0]).'.php';

if (is_file($includeFile)) {

include($includeFile);

} else die404();



thank you in advance

There is 1 reply in this thread, which is not being displayed.
Browsing this forum thread replies is available only to premium subscribers.


Go to the premium subscriptions page to learn how to become a premium subscriber and have full access to this forum.