2016-02-05 52 views
0

我已經建立了一個自制的MVC結構的網站,而.htaccess的網站根本沒有工作。 我有我的網站的根這種「前端控制器」(index.php):MVC&RewriteRule根本不起作用

<?php 
require_once('M/connect_sql.php'); 

if (!empty($_GET['page']) && is_file('C/'.$_GET['page'].'.php')) 
{ 
    include 'C/'.$_GET['page'].'.php'; 
} 
else 
{ 
    include 'C/accueil.php'; 
} 

$bdd = NULL; 

C/是我的控制器文件夾。

我的網址的實施例: http://domain.com/?page=restaurants&city=paris&CP=75001

page參數是用於我的控制器和&city=paris&CP=75001是POST變量。

我測試了一個簡單的MVC重寫規則,它運行良好。 我嘗試了幾個重寫規則,但都沒有工作。

下面是上面的URL的例子:

<IfModule mod_rewrite.c>  
    Options +FollowSymLinks 
    RewriteEngine On 

    RewriteRule ^(.+/)/([^/.]+)/([^/.]+)/?$ /?page=$1&city=$2&CP=$3 [NC,L] 
</IfModule> 

我已經把每個文件夾(根,控制器,視圖)在.htaccess,但我不能得到它。

編輯SINCE迴應:

這是我的新的控制器,因爲Entwicklerpages發現一個安全漏洞。發現該解決方案在這裏:http://kkovacs.eu/exploiting-web-development-worst-practices-file-inclusion

if (isset($_REQUEST['page']) and ! is_null($_REQUEST['page'])) 
{ 
$page = $_REQUEST['page']; 
switch ($page) { 
    case 'restaurant': 
    include('C/restaurant.php'); 
    break; 
    case 'restaurants': 
    include('C/restaurants.php'); 
    break; 

    } 
} 
else { 
    include('C/accueil.php'); 
} 

回答

0

要知道在你執行安全漏洞的! 讓用戶決定包含哪些php文件永遠不是一個好主意。

include 'C/'.$_GET['page'].'.php'; 

https://www.owasp.org/index.php/Testing_for_Local_File_Inclusion

此外,從我的網站第二個觀點。 的$_SERVER變量PHP有一個叫做PATH_INFO

$_SERVER['PATH_INFO'] 

場如果您的網站被稱爲的index.php /你/ PARAMS /和/其它/數據

您可以通過「/」拆呢並獲取您的所有數據。 此外,這可能是更容易一些,使其花哨ModRewrite

https://secure.php.net/manual/en/reserved.variables.server.php

我用這個在我自己的網站:http://www.entwicklerpages.de/Startseite.html(德國)

+0

非常感謝Entwicklerpages你把響應時間。這真的有幫助!我設法做出了RewriteRule。對於安全漏洞,我用我找到的解決方案更新了我的帖子。你認爲現在更安全嗎? – Damien