.htaccess 文件被 apache 忽略

.htaccess 文件被 apache 忽略

我正在使用 codeigniter 中的 php 构建一个 Web 应用程序。本地主机中一切正常。然而,尽管在 httpd.conf 中覆盖了所有内容,但 .htaccess 文件仍被我的实时服务器 (RH7.1 Apache 2.4) 忽略。我已重新启动 Apache 服务,但没有任何效果。知道会出什么问题吗?

.htaccess:

RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|js|css|uploads|favicon.png)
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]

strpos(shell_exec('/usr/local/apache/bin/apachectl -l'), 'mod_rewrite') !== false

返回 1。

httpd.conf 值:

# Further relax access to the default document root:
<Directory "/var/www/html">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride All

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

。 。 。 。 。

#
# The following lines prevent .htaccess and .htpasswd files from being
# viewed by Web clients.
#
<Files ".ht*">
    Require all denied
</Files>

答案1

问题似乎出在 .htaccess 内容上。我将其替换为:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule>

它开始工作正常。我在这方面有点新。谁能告诉我为什么它之前不起作用以及为什么它可以使用新的 .htaccess 文件内容?

答案2

谁能告诉我为什么它之前不起作用以及为什么它可以使用新的 .htaccess 文件内容?


RewriteRule ^(.*)$ ./index.php/$1 [L]

您的原始.htaccess文件将请求的 URL 路径作为附加文件的一部分传递路径名信息在 URL(又名PATH_INFO)上。例如。/index.php/foo/bar

要在 Codeigniter 中启用此功能,您可能必须在以下位置显式设置config.php

$config['uri_protocol']  = 'PATH_INFO';

但是,请注意,PATH_INFO也可以使用该指令在 Apache 服务器配置中禁用它AcceptPathInfo

RewriteRule ^(.*)$ index.php?/$1 [L]

而您的“新”.htaccess文件将请求的 URL 路径作为请求参数反而。例如。/index.php?/foo/bar(注意附加?)。这可能是 Codeigniter 的默认配置,并且无论服务器配置如何,都将始终有效。

相关内容