Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,147,921 members, 7,799,123 topics. Date: Tuesday, 16 April 2024 at 03:37 PM

How To Route Pages To The Index Page And Using Htaccess - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / How To Route Pages To The Index Page And Using Htaccess (656 Views)

See Best Time To Update Your Social Media Pages To Get More Traffic In 2018 / How To Set Up Rules And Redirects In .htaccess [http:// To Https://] / I Need Facebook Pages To Advertise On. (2) (3) (4)

(1) (Reply) (Go Down)

How To Route Pages To The Index Page And Using Htaccess by falconxxx(m): 5:25pm On Dec 21, 2016
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:
http://foam.com/sammy [a user profile]
http://foam.com/sammy/pictures [sammy's picture page]
http://form.com/about [the site's about page]

i want everything to be the index page
not this
http://foam.com/user/sammy [x]
http://foam.com/usersammy/pictures
http://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
Re: How To Route Pages To The Index Page And Using Htaccess by Nobody: 6:36pm On Dec 21, 2016
9ice one
Re: How To Route Pages To The Index Page And Using Htaccess by falconxxx(m): 9:34pm On Dec 21, 2016
eduj:
9ice one

Re: How To Route Pages To The Index Page And Using Htaccess by webdeveloperqx: 9:08am On Dec 22, 2016
falconxxx:
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:
http://foam.com/sammy [a user profile]
http://foam.com/sammy/pictures [sammy's picture page]
http://form.com/about [the site's about page]

i want everything to be the index page
not this
http://foam.com/user/sammy [x]
http://foam.com/usersammy/pictures
http://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
one single line takes care of all these in LARAVEL / CODE-IGNITER

1 Like

(1) (Reply)

Web Design Company / Newly Created Ultimate Cycler Whatsapp Group / How To Move Domain Name From. Tk To .com

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 21
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.