How to create a htaccess file for my php routes

-2

I have this following route file which works fine on local server but when deployed on live server
it only shows the index page.

<?php   
$router->get('','controllers/index.php');
$router->get('about','controllers/about.php');               
$router->get('contact','controllers/contact.php');      
$router->post('contact','controllers/contact.php');
$router->get('events','controllers/events.php');              // Get all events 
$router->get('event/detail','controllers/single-event.php'); //Get single events 
$router->get('news','controllers/news.php');                 // Get all news 
$router->get('news/detail','controllers/single-news.php');   // Get single news article 
$router->get('members','controllers/members.php');          // Get all members
$router->get('blog','controllers/blog.php');                // Get all article
$router->get('blog/detail','controllers/single-blog.php');  // Get single blog article
$router->get('blog/tag','controllers/all-blog-tags.php');  // Get all blog item based on tag
$router->get('oops','controllers/error-page.php');         // Get 404 Page ?>

I searched for a while but can't add htaccess because I have little to no information about it. I'm working with pure php no frameworks are envolved, I implemented a mvc approach and thus the routes issue. When I'm working on local server I can browse to every page but on the live server only the first page shows up. I get 404 error on the other pages. The server requires the full path to the page. here is the 404 error

Detailed Error Information:
Module     IIS Web Core
Notification       MapRequestHandler
Handler    StaticFile
Error Code     0x80070002
Requested URL      http://rozhana.com:80/members
Physical Path      C:\rozhana\members
Logon Method       Anonymous
Logon User     Anonymous

The path to members page is like this public/views/members.php but the server does not recognize that. Router class

<?php

class Router
{
    protected $routes = [
        'GET' => [],
        'POST' => [],
    ];

    public static function load($file)
    {
       $router = new static;
        require $file;
        return $router;
    }
    
    public function get($uri,$controller)
    {
        $this->routes['GET'][$uri] = $controller;
    }
    public function post($uri,$controller)
    {
        $this->routes['POST'][$uri] = $controller;
    }

  
    public function direct($uri,$requestType)
    {
        if(array_key_exists($uri,$this->routes[$requestType])){
             return $this->routes[$requestType][$uri];
        }
       return $this->routes['GET']['oops'];
    }
}

Request class

<?php

class Request
{
    public static function uri()
    {
      
          return ( trim(parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH),'/'));
     }

    public static function method()
    {
        return ($_SERVER['REQUEST_METHOD']);
    }
}
php
server
asked on Stack Overflow Aug 31, 2020 by Daisy Lily • edited Aug 31, 2020 by Daisy Lily

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0