Question

So with a little help I was able to get my first mvc framework up and running locally. Now that I've put it up on the server I'm not having any luck. I believe it's a configuration issue but I can't seem to figure it out.

Here's a Gif of what it should look like on the server but this is running it locally.

enter image description here

I used a Rewrite file but my friend helped me with it so I don't fully understand what it is doing. I read the RewriteBase documentation as well as the Apache mod_rewrite documentation but am still pretty confused. So with these in mind:

Could someone please help me understand this Rewrite file a little more.

  • ie:

    • How does %{REQUEST_FILENAME} work/what does it do and how does it work with the RewriteRule for both !-f and !-f?
    • What does [NC, L] do?
  • What I know:

    • '-d' (is directory) Treats the TestString as a pathname and tests whether or not it exists, and is a directory.
    • '-f' (is regular file) Treats the TestString as a pathname and tests whether or not it exists, and is a regular file.

.htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [NC,L]

tinyMvc.php (application/tinyMvc.php)

(Please ignore the fact that this is named tinyMvc. It has nothing to do with tinyMvc.)

<?php

    include_once 'load.php';
    // require_once '../../webconfig.php';
    // include_once 'models/model.php';
    // include_once 'controllers/controller.php';

    // Local
    // define ('URL_ROOT', 'http://localhost/');

    // Remote
    define ('URL_ROOT', 'http://tomcat.cit.ipui.edu/alecory/Spring-2014/Assignment%202/');

    // define ('URI', $_SERVER['REQUEST_URI']);
    // Outputs for:
    //    Local  = /register                                        (ex: register form)
    //    Remote = /alecory/Spring-2014/CIT-31300/Assignment%202/views/register.php
    define('URI', '/register'); // <= this is where I could set it myself and it would
                                    # reroute the URL from
                                    # /Assignment%202/views/register.php   To         
                                    # /Assignment%202/ 
                                    # (only showing /Assignment%202/ in the URL)

    define ('DOC_ROOT', $_SERVER['DOCUMENT_ROOT']);
    //    Local  = /Applications/MAMP/htdocs/CIT-31300/Assignment 2 (ex: register form)
    //    Remote = /var/www/                                        (ex: register form)

    function autoloader($class)
    {
        include_once strtolower($class) . 's/' . $class . '.php';
    }
    spl_autoload_register('autoloader');

    new Controller();

?>

controller.php (application/controllers/controller.php)

class Controller
{
    public $load;
    public $model;

    function __construct()
    {
        // Make
        $this->load  = new Load();
        $this->model = new Model();

        // Set the $page = current view/page
        $page = ltrim(URI, '/');

        // Set default page to index
        if (empty($page))
        {
            $page = 'index';
        }

        // Load the Pages
        if (method_exists($this, $page))
        {
            // die(get_include_path());
            require_once DOC_ROOT . '/views/inc/header.php';
            $this->$page();
            require_once DOC_ROOT . '/views/inc/footer.php';
        } 
        else
        {
            require_once DOC_ROOT . '/views/inc/header.php';
            $this->notFound();
            require_once DOC_ROOT . '/views/inc/footer.php';
        }

    }

    // Functions to load the various views
    function index()
    {
        // $data = $this->model->my_user_info();
        $this->load->view('myview.php', $data); 
    }

    function login()
    {
        $this->load->view('login.php', $data); 
    }

    function register()
    {
        $this->load->view('register.php', $data);
    }

    function notFound()
    {
        die('not found');
    }
}

?>

No correct solution

OTHER TIPS

This is partial answer that only talks about what the .htaccess file is doing:

# Tell Apache you want to utilize the rewrite module
RewriteEngine On

# Specify the url prefix, only really necessary when resources are not 
#   relative to your web root
RewriteBase /

# Compare the requested path to the files in the web root and if it doesn't 
#   match a file the condition is meet so check the next condition. 
RewriteCond %{REQUEST_FILENAME} !-f

# Compare the requested path to the directories in the web root and if it 
#   doesn't match a directory the condition is meet so check the next condition. 
RewriteCond %{REQUEST_FILENAME} !-d

# No more conditions so execute this rewrite rule: rewrite everything to be 
#   'index.php'. NC is to ignore case ( not really necessary since you are 
#   already rewriting everything ), and L signifies that this should be the 
#   last rule executed.
RewriteRule ^ index.php [NC,L]

The mod_rewrite docs are a pain to understand when you're new, but they do cover all the rules and conditions.

Your issue could be the difference in virtual host not having AllowOverride (http://httpd.apache.org/docs/current/mod/core.html#allowoverride). This is important because if AllowOverride isn't set correctly, your .htaccess file won't be read.

Check your apache versions as well, AllowOverride's default value has recently changed.

This is the default values from the docs: AllowOverride None (2.3.9 and later), AllowOverride All (2.3.8 and earlier)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top