" //"在主域名前面的符号:" site.com.br //"

时间:2017-08-23 02:51:07

标签: php .htaccess model-view-controller

我已经建立了一个自制的mvc,看看下面的代码:

\ WWW \的index.php

<?php

define('CONTROLLERS', 'app/controllers/');
define('VIEWS', 'app/views/');
define('MODELS', 'app/models/');
define('HELPERS', 'system/helpers/');

require_once 'system/system.php';
require_once 'system/controller.php';
require_once 'system/model.php';

function __autoload($file) {

if (strpos($file, 'Model') !== false) {

    $filenm = explode('Model', $file);
    $stringfile = strtolower($filenm[0]);
    $file = $stringfile . 'Model';

    if (file_exists(MODELS . $file . '.php')) {
        require_once MODELS . $file . '.php';
    } else {
        $msg = 'Model não encontrado';
        die($msg);
    }
}
if (strpos($file, 'Helper') !== false) {

    $filenm = explode('Helper', $file);
    $stringfile = strtolower($filenm[0]);
    $file = $stringfile . 'Helper';

    if (file_exists(HELPERS . $file . '.php')) {
        require_once HELPERS . $file . '.php';
    } else {
        $msg = 'Helper não encontrado';
        die($msg);
    }
}
}

$start = new System;
$start->run();

\ WWW \应用\控制器\ indexController.php

<?php

class IndexController extends Controller {

public function index_action() {

    $this->view('index');


}

}

WWW \ SYSTEM \ system.php

<?php

class System {

private $_url;
private $_explode;
public $underline_controller = FALSE;
public $_controller;
public $_action;
public $_params;
public $_params_no_action;

public function __construct() {
    $this->setUrl();
    $this->setExplode();
    $this->setController();
    $this->setAction();
    $this->setParams();
}

private function setUrl() {

    $_GET['url'] = (isset($_GET['url']) ? $_GET['url'] : 'index/index_action');
    $this->_url = $_GET['url'];
}

private function setExplode() {

    $this->_explode = explode('/', $this->_url);
}

private function setController() {
    //o controller sempre será o primeiro nome do array acima.
    $this->_controller = $this->_explode[0];
    if (strpos($this->_controller, '_') !== false)
        $this->underline_controller = TRUE;

    $this->_controller = str_replace("-", "_", $this->_controller);
}

private function setAction() {

    $ac = (!isset($this->_explode[1]) || $this->_explode[1] == null || $this->_explode[1] === 'index' ? 'index_action' : $this->_explode[1]);
    $this->_action = $ac;

    $this->_action = str_replace("-", "_", $this->_action);

}

private function setParams() {
    unset($this->_explode[0], $this->_explode[1]);
    if (end($this->_explode) == null)
        array_pop($this->_explode);


    $i = 0;
    if (count($this->_explode) > 1) {
        foreach ($this->_explode as $val) {
            if ($i % 2 == 0) {
                $ind[] = $val;
            } else {
                $value[] = $val;
            }
            $i++;
        }
    } else {
        $ind = array();
        $value = array();
    }
    if (count($ind) == count($value) && !empty($ind) && !empty($value)) {
        $this->_params = array_combine($ind, $value);
    } else {
        $this->_params = array();
    }


    if (count($this->_explode) == 1) {
        if (!empty($this->_explode[2])) {
            $this->_params_no_action = array(0 => $this->_action);
            $this->_action = $this->_explode[2];
        }
    }
    if (count($this->_explode) == 0) {
        $this->_params_no_action = array(0 => $this->_action);
    }
}

public function getParam($name = null) {
    if ($name != null)
        return $this->_params[$name];
    else
        return $this->_params;
}

public function getParamNoAction($name = null) {
    if ($name != null)
        return $this->_params_no_action[$name];
    else
        return $this->_params_no_action;
}

public function run() {

    $controller_path = CONTROLLERS . strtolower($this->_controller) . 'Controller.php';
    if (!file_exists($controller_path) || $this->underline_controller) {
        /* controller not exist */
        header('HTTP/1.1 404 Not Found');
        header("Refresh:0; url=erro404");
    }

    require_once $controller_path;
    $this->_controller = ucfirst($this->_controller) . 'Controller';
    $app = new $this->_controller();

    //se a controller for curso, permite, caso nao, redireciona para o 404...
    if (!method_exists($app, $this->_action))
        $this->_action = 'index_action';

    $action = $this->_action;
    $app->$action();
}

}

WWW \ SYSTEM \ Controller.php这样

<?php

class Controller extends System {

protected function view($nome, $vars = null) {
    if (is_array($vars) && count($vars) > 0)
        extract($vars, EXTR_PREFIX_ALL, 'view');

    return require_once VIEWS . $nome . '.phtml';
}

}

WWW \ htaccess的

<IfModule mod_rewrite.c>


RewriteEngine on

RewriteCond %{SCRIPT_FILENAME} !-f

RewriteCond %{SCRIPT_FILENAME} !-d

RewriteRule ^(.*)$ index.php?url=$1

# redirect from http to https
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</IfModule>

<IfModule mod_deflate.c>

SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.avi$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.mov$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.mp3$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.mp4$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.rm$ no-gzip dont-vary

<FilesMatch "\.(js|css|html|xml|x?html?|php)$">
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE image/svg+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/atom_xml
    AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/json
    AddOutputFilterByType DEFLATE application/x-httpd-php
    AddOutputFilterByType DEFLATE application/x-httpd-fastphp
    AddOutputFilterByType DEFLATE application/x-httpd-eruby
    AddOutputFilterByType DEFLATE text/html
</FilesMatch>

#DeflateCompressionLevel 5

BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

#DeflateFilterNote Input instream
#DeflateFilterNote Output outstream
#DeflateFilterNote Ratio ratio

#LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate

</IfModule>

<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On 
# Default directive
ExpiresDefault "access plus 1 month"
# My favicon
ExpiresByType image/x-icon "access plus 1 year"
# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
# CSS
ExpiresByType text/css "access plus 1 month"
# Javascript
ExpiresByType application/javascript "access plus 1 year"
</IfModule>

我刚刚安装了https,但问题是我输入&#34; site.com.br&#34;在浏览器中,URL被重写为&#34; https://site.com.br//&#34;在浏览器中,附加符号&#34; //&#34;正在使用,当我在浏览器中键入https://site.com.br时,不添加符号&#34; //&#34; 我无法找到问题所在,所以我来这里寻求帮助。有谁可以帮助我吗?谢谢!

1 个答案:

答案 0 :(得分:0)

您需要先进行重定向,然后再进行路由,否则您将在重定向中获取已经重写的URI。所以保持你的ModRewrite指令如下:

<IfModule mod_rewrite.c>
    RewriteEngine on

    # redirect from http to https
    RewriteCond %{HTTPS} !on
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]

    RewriteCond %{SCRIPT_FILENAME} !-f

    RewriteCond %{SCRIPT_FILENAME} !-d

    RewriteRule ^(.*)$ index.php?url=$1 [L]
</IfModule>