简体   繁体   English

Apache-htaccess 500内部服务器错误

[英]Apache - htaccess 500 internal server error

I'm learning how to make PHP MVC applications, and the tutorial I followed involved creating two .htaccess files - one to prevent a user from navigating to the application folders and files; 我正在学习如何制作PHP MVC应用程序,并且遵循的教程涉及创建两个.htaccess文件-一个用于防止用户导航到应用程序文件夹和文件。 the other to rewrite how the URL is presented in order for me to parse and retrieve controllers, methods and parameters. 另一个重写URL的显示方式,以便我解析和检索控制器,方法和参数。

When deploying these files on my XAMPP server, they work correctly. 在我的XAMPP服务器上部署这些文件时,它们可以正常工作。 However, when deploying these files on to my university server via FTP, the 500 Internal Server Error appears, and removing these files will allow me to see my application. 但是,通过FTP将这些文件部署到我的大学服务器上时,出现500 Internal Server Error ,并且删除这些文件将使我能够看到我的应用程序。 But I need these files otherwise I can't call the appropriate controllers, models and views. 但是我需要这些文件,否则我将无法调用适当的控制器,模型和视图。

I have searched around for the answer, and many solutions say that there could be syntax errors, which I don't believe I have any as it works on the XAMPP instance just fine. 我一直在寻找答案,许多解决方案都说可能存在语法错误,我不认为我有任何错误,因为它可以在XAMPP实例上正常工作。 The other answers were that the server itself may not be configured to allow me to upload such files or something similar. 其他答案是服务器本身可能未配置为允许我上载此类文件或类似内容。

I do not have access to the configuration of my university's server, which I know may hinder any solutions you could provide to me. 我无权访问大学服务器的配置,我知道这可能会妨碍您提供给我的任何解决方案。 So, if there are any PHP solutions to at least let me parse the URL the way I need to, I'll be happy to drop the .htaccess files. 因此,如果有任何PHP解决方案至少可以让我以所需的方式解析URL,我将很乐意删除.htaccess文件。

The first .htaccess file, located within the app directory of the application: 第一个.htaccess文件,位于应用程序的应用程序目录中:

Options -Indexes

The second .htaccess file; 第二个.htaccess文件; located within the public directory of my application: 位于我的应用程序的公共目录中:

Options -MultiViews
RewriteEngine On

RewriteBase /~21249593/Internet_Technologies/Element_2/public

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

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

The PHP class used to parse the URL to call the appropriate controller and its method, I have posted the full logic so you can understand how the parsing works: 用来解析URL调用适当的控制器和方法的PHP类,我已经发布了完整的逻辑,所以你可以理解解析如何工作的:

class App {
    protected $controller = 'home';
    protected $method = 'index';
    protected $params = array();

    public function __construct() {
        $url = $this->parseUrl();

        if (file_exists('../app/controllers/' . $url[0] . '.php')) {
            $this->controller = $url[0];
            unset($url[0]);
        }

        require_once '../app/controllers/' . $this->controller . '.php';

        $this->controller = new $this->controller;

        if (isset($url[1])) {
            if (method_exists($this->controller, $url[1])) {
                $this->method = $url[1];
                unset($url[1]);
            }
        }

        $this->params = $url ? array_values($url) : array();

        call_user_func_array([$this->controller, $this->method], $this->params);
    }

    public function parseUrl() {
        if (isset($_GET['url'])) {
            return $url = explode('/', filter_var(rtrim($_GET['url'], '/'), FILTER_SANITIZE_URL));
        }
    }
}

I appreciate any help you can give me here 我感谢您可以在这里给我的任何帮助

EDIT 编辑

I should've stated that XAMPP is using 2.4.9 Apache, the FTP is 2.2.3 我应该说XAMPP使用2.4.9 Apache,FTP是2.2.3

It does seem mod_rewrite or .htaccess usage in general is forbidden for your account. 似乎您的帐户通常禁止使用mod_rewrite或.htaccess。 You should ask your administration if that can be changed, if not, you cannot use mod_rewrite urls. 您应该询问您的管理员,是否可以更改,否则不能使用mod_rewrite url。

What you can anyway do is to use urls like http://yourdomain/index.php?url=something , those should work regardless. 无论如何,您可以使用http://yourdomain/index.php?url=something ,无论该http://yourdomain/index.php?url=something如何工作。 For anything "cleaner" you would need a server configuration change. 对于任何“清洁”的东西,您都需要更改服务器配置。

Well, if you can't rewrite the URL you have to change the links to your application to the format your application needs them. 好吧,如果您无法重写URL,则必须将应用程序的链接更改为应用程序需要它们的格式。

So insteaf of linking to mydomain.com/controller/method/param1/param2... you need to link to mydomain.com/index.php?url=controller/method/param1/param2.. 因此,如果您不打算链接到mydomain.com/controller/method/param1/param2 ...,则需要链接到mydomain.com/index.php?url=controller/method/param1/param2.。

The links are not that pretty anymore, but they will work. 链接不再那么漂亮了,但是它们会起作用。

Try to change 'Encoding' of '.htaccess' file. 尝试更改“ .htaccess”文件的“编码”。 It worked for me. 它为我工作。 I used ANSI. 我使用ANSI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM