禁止访问:您没有权限访问该服务器上的 / 目录。

68
今天我想做的就是编写一个将URL重定向到子目录的重定向规则,例如: 您输入URL:example.com,然后被重定向到example.com/subfolder 这是一个非常简单的需求。我试图在互联网上寻找解决方案。互联网告诉我在htdocs根目录下添加.htaccess文件,并添加以下内容:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^$ subfolder [L]

我这样做了。但显然没有成功,他们没有告诉我我必须在httpd.conf中取消注释模块:

LoadModule rewrite_module modules/mod_rewrite.so

我也尝试了这个方法,但没有成功。他们没有告诉我需要更改我的 httpd.conf 文件以启用 .htaccess 文件:

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
</Directory>

DocumentRoot "c:/Apache24/htdocs"
<Directory "c:/Apache24/htdocs">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

再次没有成功,因为当我输入URL时,出现了以下错误:

Forbidden 您无权访问此服务器上的/。

现在我陷入困境,我在互联网上找不到更多解决方案。 我只是在我的Windows 7机器上运行Apache 2.4,出于私人原因。


可能是Apache权限被拒绝的重复问题。 - Alastair Irvine
在Apache2.4上,我该在哪里找到httpd.conf文件? - Black
6个回答

171

通过Error with .htaccess and mod_rewrite找到了解决方法。
对于Apache 2.4和所有*.conf文件(例如httpd-vhosts.conf,http.conf,httpd-autoindex.conf等),请使用以下内容:

Require all granted

而不是

Order allow,deny
Allow from all

Apache 2.4已弃用OrderAllow指令。


天啊,我刚从CentOS迁移过来,很高兴看到这个。谢谢! - robm
22
当场废弃!- Apache 错误信息会更好 - Andrew Atkinson
3
完全同意@AndrewAtkinson的观点。多年使用Apache2,但现在是相同版本,不同的配置。 - Ant
2
抱歉问一下,我们应该把那行代码放在哪里? - nyxee
谢谢!在我的情况下,是 /etc/apache2/conf-available/security.conf 拒绝了访问。 - Jehan Zeb
显示剩余3条评论

12

工作方法 {如果除了配置问题没有其他问题}

默认情况下,Apache不会限制ipv4(常见的外部ip)访问。

可能会限制访问的是“httpd.conf”中的配置(或者根据您的apache配置,“apache2.conf”)

解决方案:

替换所有:

<Directory />
     AllowOverride none
    Require all denied

</Directory>

随着

<Directory />
     AllowOverride none
#    Require all denied

</Directory>

因此移除了对Apache的所有限制

C:/wamp/www/目录下,用Require all granted替换Require local

<Directory "c:/wamp/www/">
    Options Indexes FollowSymLinks
    AllowOverride all
    Require all granted
#   Require local
</Directory>

3
解决方案非常简单。如果您尝试使用本地IP地址访问服务器,并且出现“Forbidden You don't have permission to access / on this server”等错误,请按以下步骤操作:
只需打开httpd.conf文件(在我的情况下是C:/wamp/bin/apache/apache2.2.21/conf/httpd.conf)。
搜索
<Directory "D:/wamp/www/"> .... ..... </Directory>
Allow from 127.0.0.1 替换为 Allow from all 保存更改并重新启动服务器。
现在,您可以使用IP地址访问服务器了。

2
问题出现在https.conf文件中!
# Virtual hosts
# Include conf/extra/httpd-vhosts.conf

错误发生在hash(#)被移除或混乱的时候。这两行应该像上面展示的那样出现。

4
请规范回答格式。 - Jonathan Eustace
搞什么了? - aircraft

1

我在 Apache/2.2.15 (Unix) 上找到了解决方案。

感谢 @QuantumHive 的答案:

首先: 我找到了所有的

Order allow,deny
Deny from all

替代

Order allow,deny

Allow from all

然后:

我设置了

#
# Control access to UserDir directories.  The following is an example
# for a site where these directories are restricted to read-only.
#
#<Directory /var/www/html>
#    AllowOverride FileInfo AuthConfig Limit
#    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
#    <Limit GET POST OPTIONS>
#        Order allow,deny
#        Allow from all
#    </Limit>
#    <LimitExcept GET POST OPTIONS>
#        Order deny,allow
#        Deny from all
#    </LimitExcept>
#</Directory>

取消前一个“#”标注

#
# Control access to UserDir directories.  The following is an example
# for a site where these directories are restricted to read-only.
#
<Directory /var/www/html>
    AllowOverride FileInfo AuthConfig Limit
    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
    <Limit GET POST OPTIONS>
        Order allow,deny
        Allow from all
    </Limit>
    <LimitExcept GET POST OPTIONS>
        Order deny,allow
        Deny from all
    </LimitExcept>
</Directory>

ps. 我的WebDir是: /var/www/html


0

这对我在Mac OS Mojave上有效:

<Directory "/Users/{USERNAME}/Sites/project">
    Options +Indexes +FollowSymLinks +MultiViews
    AllowOverride All
    require all granted
</Directory>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接