Apache重写:找不到文件

格兰特·马丁

我正在使用apache的.htaccess将URL重写为应用程序中不同模块的应用程序。当我尝试浏览到我的应用程序时,出现以下错误:

在此服务器上找不到请求的URL /var/www/dynamicsuite/index.php。

但是/var/www/dynamicsuite/index.php确实存在!我尝试使用chmod 777来查看是否是权限问题,但仍然无法正常工作。

这是一般的文件布局以及我要完成的工作:

/var/www/app                 - Main Directory
/var/www/app/index.php       - This is what I rewrite too
/var/www/app/modules/login   - The login form
/var/www/app/modules/home    - The homepage on login
/var/www/app/modules/error   - Error page if something goes wrong

当用户转到该应用程序时,它将查看他们是否已登录并将其重定向到适当的模块。我使用mod_rewrite是因为我不希望用户必须指定长网址,例如mysite.com/app/modules/login相反,我正在这样做,所以它是mysite.com/app/loginURL中模块名称之后的所有内容,我想使用我编写的类/方法作为变量进行处理。例:

mysite.com/app/error/404/time/user/etc

mysite.com/app/modules/error?error=404&time=12345&user=username&etc=asdf

这是我在.htaccess文件中使用的代码:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?url=$1 [QSA,PT]

AddDefaultCharset UTF-8

注意:我正在使用PT标志,因为我正在为应用程序使用别名。

将Ubuntu 14.04与apache2,mod_rewrite,mod_alias结合使用

您可以在此处看到它:

https://test.walnutcreekhardware.com/ds/

这是其他使用的文件:

配置

$cfg["install_dir"] = array("ds", "employee", "dynamicsuite");

index.php

// Required Scripts
require_once("config/config.php");

function __autoload($class) {
    require_once("core/lib/$class.class.php");
}

if(!dsDatabase::dbCheck()) {
    dsInstance::genericError(1);
    exit;
}

// Process the URI and direct the user to the proper module
elseif(!isSet(dsInstance::getUri()[0])){

    // If a session if found, skip the login page and render the homepage
    if(dsSession::checkSession() === true) {
        header("Location: home");
    }

    // If no session is found, render the login page
    else {
        header("Location: login");
    }

}

// If the module exists, render it
elseif(file_exists("modules/" . dsInstance::getUri()[0])) {
    require_once("modules/" . dsInstance::getUri()[0] . "/index.php");
}

// If no conditions are met, render the error page
else {
    dsInstance::genericError(404);
}

getUri()函数:

public static function getUri() {

    global $cfg;

    if(strpos($_SERVER["REQUEST_URI"], "?") != false) {
        $uri = explode("/",      trim(substr($_SERVER["REQUEST_URI"],0,strpos($_SERVER["REQUEST_URI"],"?")),"/"));
    } else {
        $uri = explode("/", trim($_SERVER["REQUEST_URI"], "/"));
    }

    $search = in_array($uri[0], $cfg["install_dir"]);

    if($search === true) {
        return array_splice($uri, 1, count($uri));
    } else {
        return $uri[0];
    }

}

dbCheck()函数:

public static function dbCheck() {

    global $cfg;

    try {

        $db = new PDO($cfg['db_type'] . ":host=" .
                      $cfg['db_host'] . ";dbname=" .
                      $cfg['db_name'],
                      $cfg['db_user'],
                      $cfg['db_pass']);

    } catch (Exception $e) {
        return 0;
    }

    return 1;
}

checkSession()函数:

public static function checkSession() {

    if(isSet($_SESSION["DS_SESSION"])) {
        return true;
    } else {
        return false;
    }

}

genericError()函数:

public static function genericError($code) {
    $location = dsInstance::getUri()[0];
    $time = time();
    header("Location: error/$code/$location/$time");
}

谢谢!

格兰特·马丁

我似乎已经解决了该问题(感谢zx81,将问题缩小为别名!)

通过将mod_rewrite指令RewriteBase添加到my。htaccess文件现在似乎可以正常工作了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章