MAMP .htaccess 文件不起作用

MAMP .htaccess 文件不起作用

我正在寻找如何在 MAMP 2.1.2 中启用 .htaccess 文件

我有以下设置:

httpd配置文件

LoadModule rewrite_module modules/mod_rewrite.so
...
#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
# MAMP DOCUMENT_ROOT !! Don't remove this line !!
DocumentRoot "/Applications/MAMP/htdocs"

#
# Each directory to which Apache has access can be configured with respect
# to which services and features are allowed and/or disabled in that
# directory (and its subdirectories). 
#
# First, we configure the "default" to be a very restrictive set of 
# features.  
#
<Directory />
    Options Indexes FollowSymLinks
    AllowOverride All
</Directory>

#
# Note that from this point forward you must specifically allow
# particular features to be enabled - so if something's not working as
# you might expect, make sure that you have specifically enabled it
# below.
#

#
# This should be changed to whatever you set DocumentRoot to.
#
<Directory "/Applications/MAMP/htdocs">
    #
    # 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.2/mod/core.html#options
    # for more information.
    #
    Options All

    #
    # 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.
    #
    Order allow,deny
    Allow from all

</Directory>
...
#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>

#
# AccessFileName: The name of the file to look for in each directory
# for additional configuration directives.  See also the AllowOverride 
# directive.
#
AccessFileName .htaccess

.htaccess 文件(已在线上网站运行):

RewriteEngine on
RewriteRule ^([a-z\-]+)$ /index.php?page=$1 [L]

当我导航到 /foo 时的页面输出

Not Found

The requested URL /index.php was not found on this server.

我尝试在 .htaccess 文件中添加以下行 因为我的网站位于:localhost/mysite/ 但没有任何效果。

RewriteBase /mysite/

访问 localhost/mysite/index.php?page=foo 确实有效..

有任何想法吗?

答案1

在这里,您将使用 apache 的两个功能:

  1. .htaccess 文件,由指令管理AllowOverride
  2. rewrite_mod 是 Apache 的一个模块。

要启用 .htaccess 文件管理,您需要配置“允许覆盖”指令。另外,检查访问文件名未修改(否则,您应该将 .htaccess 文件重命名为该指令中配置的文件。

要使用 url 重写,rewrite_mod必须加载该模块。

由于您收到 404 错误,这表明直接AllowOverride配置不正确。之后,如果您收到 500 错误,则意味着内容.htaccess(因此您的重写配置)有错误。

要了解有关重写操作的更多详细信息,您还可以查看重写日志指示。

祝你好运 !

答案2

最后我发现问题不是服务器配置,而是 .htaccess 文件。

我变了:

RewriteEngine on
RewriteRule ^([a-z\-]+)$ /index.php?page=$1 [L]

到:

RewriteEngine on
RewriteRule ^([a-z\-]+)$ index.php?page=$1 [L]

并且这确实起到了作用。

谢谢大家的帮助。

答案3

vi /etc/hosts

您应该在这里看到本地主机服务器名称。

然后做:

vi /etc/apache2/sites-avaiable/000-default.conf

除了000-default.conf,您还可以找到类似的内容。您的网站在这里配置了吗?您应该在这里看到类似这样的内容:

<VirtualHost *:80>
ServerName localhost

    ServerAdmin webmaster@localhost
    DocumentRoot /Applications/MAMP/htdocs
            <Directory />
                            Order Deny,Allow
                            Deny from all
                            Options None
                            AllowOverride None
            </Directory>
            <Directory /Applications/MAMP/htdocs>
                            Options +FollowSymLinks +MultiViews
                            AllowOverride All
                            Order allow,deny
                            allow from all
                            Require all granted
            </Directory>

</VirtualHost>

你可能AllowOverride none在这里有任何东西。

更新:

我觉得我找到了问题所在。当你这样做时:

RewriteEngine on
RewriteRule ^([a-z\-]+)$ /index.php?page=$1 [L]

您不允许/在正则表达式中使用。因此,请尝试以下操作:

RewriteEngine on
RewriteRule ^(mysite/)?([a-z\-]+)$ /$1index.php?page=$1 [L]

或者

RewriteEngine on
RewriteRule ^([a-z\-]+)/([a-z\-]+)$ /$1/index.php?page=$2 [L]

现在,它应该可以在您的本地主机上为您工作。

相关内容