【问题标题】:Place index.php under public folder in ubuntu 16.04 LTS using .htaccess使用 .htaccess 将 index.php 放在 ubuntu 16.04 LTS 的公共文件夹下
【发布时间】:2018-04-05 00:32:23
【问题描述】:

我已经尝试了很多教程,在stackoverflow和其他网站上搜索和研究,但我仍然无法解决这个问题。 这是它。 这是我的文件夹结构以及 .htaccess 和 index.php 文件:

file structure | .htaccess | index.php

当我尝试访问内置服务器的 php (sudo php -S localhost:800) 时,我得到了这个:

Php built-in server

使用 apache2 时,我得到如下文件夹结构:

Apache Server

我也尝试将 DirectoryIndex 更改为 public/index.php,但是在键入查询字符串时(SEO 友好,使用斜杠而不是 ex.:?a=2)我收到错误,服务器显示例如:

在此服务器上找不到请求的资源 /mvc-oop/home/id/2。

【问题讨论】:

    标签: php apache .htaccess mod-rewrite model-view-controller


    【解决方案1】:

    您使用的服务器似乎未配置为指向包含您的 index.php 条目文件的正确目录。

    首先,.htaccess 文件被 Apache Web 服务器用作配置文件。所有 Web 服务器软件都无法理解或读取此配置文件。换句话说:.htaccess 适用于 Apache,但不适用于您的 php 服务器。

    在 .htaccess 文件中修改最后一行,使其包含以 ./ 开头的有效相对路径

    ReWriteRule ^ ./public/index.php?q=$1 [QSA,L]
    

    ./public 翻译为:“从当前目录开始,下降到名为 public... 的子文件夹”

    【讨论】:

    • 嘿兄弟,非常感谢,这么说吧,但它只能在apache上工作,不能在php内置服务器上工作,但那太棒了,再次感谢你:DirectoryIndex public/index.php RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-l ReWriteRule ^ ./public/index.php?q=$1 [QSA,L]跨度>
    • 是的,只是在 apache 中!不过暂时够用了。
    • Gustavo,.htaccess 文件是 apache 唯一的重定向解决方案。它不适用于 php 服务器。
    • 哦,是的,明白了!
    猜你喜欢
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    • 1970-01-01
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多