php - User friendly URLs without htaccess -
i want create friendly urls website script using php, right im using query style (ex: index.php?location=register) , convert them this:
https://www.sitename.com/index.php/register
right im using $_get based function parse , include php script based on $_get value.
$includedir = ".".directory_separator."assets/controllers".directory_separator; $includedefault = $includedir."home.php"; if(isset($_get['ajaxpage']) && !empty($_get['ajaxpage'])){ $_get['ajaxpage'] = str_replace("\0", '', $_get['ajaxpage']); $includefile = basename(realpath($includedir.$_get['ajaxpage'].".php")); $includepath = $includedir.$includefile; if(!empty($includefile) && file_exists($includepath)) { include($includepath); } else{ include($includedefault); } exit(); } if(isset($_get['location']) && !empty($_get['location'])) { $_get['location'] = str_replace("\0", '', $_get['location']); $includefile=basename(realpath($includedir.$_get['location'].".php")); $includepath = $includedir.$includefile; if(!empty($includefile) && file_exists($includepath)) { include($includepath); } else { include($includedefault); } } else { include($includedefault); }
kind regards!
why need show index.php in url?
i create url https://www.sitename.com/register
if want clean url's need use rewrite.
but need use .htaccess
or apache config
rules such this.
rewriteengine on rewritecond %{request_filename} !-f rewritecond %{request_filename} !-d rewriterule ^([^/]+)/?$ index.php?location=$1 [l]
then in php code can on location var $_get["location"]
, load page value sent.
the result of $_get["location"]
register
url , display page.
i don't suggest using multiviews can cause issues if have file , folders same name. e.g. /admin , admin.php
.
Comments
Post a Comment