【问题标题】:Can't redirect URLs to index.php with .htaccess file无法使用 .htaccess 文件将 URL 重定向到 index.php
【发布时间】:2021-04-16 23:09:18
【问题描述】:

我正在关注 Traversy Media 的 PHP OOP and MVC course。我在第 15 课上使用引导文件和核心课程。

我似乎无法将 URL 重定向到 index.php 页面。

例如:

http://localhost:8080/ohmyMVC/index.php?url=thisisURL

返回 thisisURL。但是这个

http://localhost:8080/ohmyMVC/thisisURL

返回

The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

If you think this is a server error, please contact the webmaster.

Error 404
localhost
Apache/2.4.46 (Unix) OpenSSL/1.1.1h PHP/8.0.0 mod_perl/2.0.11 Perl/v5.32.0```

这是我的核心课程:

  class Core {
    protected $currentController = 'Pages';
    protected $currentMethod = 'index';
    protected $params = [];

    public function __construct(){
      $this->getUrl();
    }

    public function getUrl(){
      echo $_GET['url'];   
    }
  } 

这是我的 bootstrap.php

<?php

// Load Libraries

require_once 'libraries/Core.php';
require_once 'libraries/Controller.php';
require_once 'libraries/Database.php';

.htaccess 在公用文件夹中:

<IfModule mod_rewrite.c>
  Options -Multiviews
  RewriteEngine On
  RewriteBase /traversymvc/public
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule  ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule>

索引.php

<?php

    require_once '../app/bootstrap.php';

//Init Core Lib

    $init = new Core;```

这是我的 httpd.conf 文件

Alias /bitnami/ "/opt/lampp/apache2/htdocs/"
Alias /bitnami "/opt/lampp/apache2/htdocs"

<Directory "/opt/lampp/apache2/htdocs">
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

【问题讨论】:

  • 是否启用了 mod_rewrite?当您删除指令周围的 IfModule 包装器时会发生什么,您是否收到任何错误? Apache 是否设置为开始读取 .htaccess 文件?
  • 删除 ifModule 包装后出现此错误:``` 找不到对象!在此服务器上找不到请求的 URL。如果您手动输入了 URL,请检查您的拼写并重试。如果您认为这是服务器错误,请联系网站管理员。错误 404 192.168.64.2 Apache/2.4.46 (Unix) OpenSSL/1.1.1h PHP/8.0.0 mod_perl/2.0.11 Perl/v5.32.0``` 我目前正在使用 XAMPP,有趣的是我试过了MAMP,这是我在该环境中删除包装器之前遇到的错误。

标签: php apache .htaccess model-view-controller xampp


【解决方案1】:

htaccess 文件中的文件夹与我要访问的文件夹不匹配。 .htaccess 文件显示 /TraversyMVC/public,而我将文件夹命名为 /ohmyMVC/public。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    • 2021-12-11
    • 2013-01-21
    • 1970-01-01
    • 2015-07-16
    • 2013-08-26
    • 1970-01-01
    相关资源
    最近更新 更多