Apache 没有重定向到 Arch Linux 中的 index.php

Apache is not redirecting to index.php in Arch Linux

我正在尝试在我的本地主机上配置一个 Wordpress 项目,但是当我在 localhost 上打开浏览器时,我只能看到我的 wordpress 项目根目录中的文件。

如果我单击 index.php 或访问 localhost/index.php,它会按预期执行 PHP。

我已经安装了 apache 2.4php 7.2php-apache 7.2

我的项目在/srv/http/mysite:

/etc/httpd/conf/httpd.conf文件中的相关行:

LoadModule rewrite_module modules/mod_rewrite.so

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

Note: Since httpd.conf is very big I showed only the lines I know to be relevant. If this is not enough I can upload the entire file on pastebin.

文件/etc/httpd/conf/extra/httpd-vhosts.conf:

<VirtualHost *:80>
    ServerAdmin webmaster@test.localhost
    DocumentRoot "/srv/http/mysite/"
    ServerName test.localhost
    ServerAlias test.localhost
    ErrorLog "/var/log/httpd/test.localhost-error_log"
    CustomLog "/var/log/httpd/test.localhost-access_log" common

    <Directory "/srv/http/mysite/">
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

文件/srv/http/mysite/.htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

我的 /etc/hosts 文件:

127.0.0.1   localhost
127.0.1.1   mycomputer_name
127.0.2.1   test.localhost
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

关于最后一个文件,我注意到如果我删除 <IfModule ...></IfModule> 站点,它会工作并重定向到 index.php,但我不想更改此文件,因为它在使用 ubuntu.

的远程服务器上的工作方式

主要问题是为什么这不起作用,但我也很高兴知道 <IfModule mod_rewrite.c> 在做什么以及为什么即使 LoadModule rewrite_module modules/mod_rewrite.so 已启用它也会被忽略。

我知道这不是一个简单的问题,但我在网上没有发现任何人遇到同样的问题。如果有任何帮助,我将不胜感激。

需要设置 Apache 以识别您的索引文件(在本例中 index.php)

您的目录中是否有以下内容

DirectoryIndex index.php

所以

<Directory "/srv/http/mysite/">
    DirectoryIndex index.php
    AllowOverride All
    Require all granted
</Directory>

在 httpd.conf 中你应该有类似...

<IfModule dir_module> 
    DirectoryIndex index.php index.htm index.html
</IfModule>

这将是您的万能后备。

然后在您的 httpd-vhosts.conf 中,许多站点描述之一可能类似于...

<VirtualHost *:8080>
    ServerAdmin admin@siteName.com
    DocumentRoot "e:/Stack/Sites/siteName/web"
    ServerName siteName
    ServerAlias siteNameUser
    <Directory "e:/Stack/Sites/SiteName/web">
        DirectoryIndex index.php
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    ErrorLog "logs/siteName.error.log"
    CustomLog "logs/siteName.access.log" common
</VirtualHost> 

注意:如果您在 httpd.conf

中包含后备默认值,则 httpd-vhosts.conf 站点声明中的 DirectoryIndex index.php 是不必要的

如果您不想使用默认端口 80,那么在任一文件中都有类似的内容:Listen 8080

对于本地主机,我使用:在 httpd.conf... Listen 127.0.0.1:8080 和 httpd-vhosts.conf Listen 8080

在您的主机文件中,您需要定义类似... 127.0.0.1 站点名称站点名称用户 www.siteName.com

当前使用:Apache/2.4.33 (Win64) PHP/7.2.4 ~ MySql 5.7.22 自制程序堆栈。 httpd-vhosts.conf 示例部分适用于 Drupal 8.x - 对于 WP,您的 DocumentRoot 和目录将有所不同。

您可以监听多个端口,例如 8080 用于开发,8081 用于测试,8082 用于生产。你也可以在你的 httpd-vhosts.conf 文件中有许多不同的站点定义。

我更喜欢在 httpd-vhosts.conf 中定义所有站点,但您可以将它们放在 httpd.conf