.htaccess从Localhost切换到远程服务器

时间:2014-02-24 00:48:35

标签: php regex .htaccess mod-rewrite url-routing

因此,通过一些帮助,我能够在本地启动并运行我的第一个mvc框架。现在我把它放在服务器上我没有运气。我认为这是一个配置问题,但我似乎无法弄明白。

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

enter image description here

我使用了Rewrite文件,但是我的朋友帮助了我,所以我不完全理解它在做什么。我读了RewriteBase documentation以及Apache mod_rewrite documentation,但我仍然很困惑。所以记住这些:

有人可以帮助我更多地了解这个重写文件。

  • 即:

    • %{REQUEST_FILENAME}如何工作/它做什么以及它如何与RewriteRule一起使用!-f和!-f?
    • [NC,L]做什么?
  • 我所知道的:

    • ' - d'(是目录) 将TestString视为路径名并测试它是否存在,并且是一个目录。
    • ' - f'(是常规文件) 将TestString视为路径名并测试它是否存在,并且是常规文件。

的.htaccess

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

tinyMvc.php (application / tinyMvc.php)

(请忽略这个名为tinyMvc的事实。它与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');
    }
}

?>

2 个答案:

答案 0 :(得分:2)

这是部分答案,只讨论.htaccess文件正在做什么:

# 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]

当你是新手时,mod_rewrite docs很难理解,但它们确实涵盖了所有的规则和条件。

答案 1 :(得分:0)

您的问题可能是虚拟主机没有AllowOverride(http://httpd.apache.org/docs/current/mod/core.html#allowoverride)的区别。这很重要,因为如果未正确设置AllowOverride,则不会读取.htaccess文件。

检查您的apache版本,最近更改了AllowOverride的默认值。

这是docs的默认值: AllowOverride无(2.3.9及更高版本),AllowOverride All(2.3.8及更早版本)