从 nginx 切换到 apache 后,我的 Routing.php 无法正常工作

My Routing.php doesnt work properly after switch from nginx to apache

我正在本地机器上做一个小项目(在 nginx docker 容器中),今天是我第一次将它上传到我的网络服务器,发现它是一个 apache 服务器。

一切正常,除了我的 router.php。

当我访问示例时。com/admin 我在我的 apache 服务器 上收到错误 404,但在我的 nginx docker 容器中我不过我得到了正确的页面

我的 error_log_apache

得到 AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
<?php
//Router.php
namespace blog;

class Router 
    public function __construct(private AdminPage           $adminPage,
                                private AdminContentPage    $adminContentPage,
                                private ArticlePage         $articlePage,
                                private VariablesWrapper    $variablesWrapper,
                                private SessionManager      $sessionManager){}

    public function getPageForUrl() : Page
    {
        switch ((string)$this->variablesWrapper->getRequestUri()){
            case '/admin':
                if ($this->sessionManager->isAuthenticated()){
                    return $this->adminContentPage;
                } else {
                    return $this->adminPage;
                }
            case preg_match('/\/([A-Za-z]\w+)\/\?article\=[0-9]{1,5}/',(string)$this->variablesWrapper->getRequestUri()): //Hello_world/?article=0
            default:
                return $this->articlePage;
        }
    }
}

因为它可能与配置有关,我必须知道具体是什么,我会给你我的 default.conf,这是我的 nginx 服务器的配置。

server {
    listen 80;
    index index.php index.twig;
    server_name localhost;
    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/html/public;



    location / {
        try_files $uri /index.php$is_args$args;
    }


    location ~ \.php$ {
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

这里是我的 apache 服务器未受影响的 httpd.conf 编辑: 显然我有权更改 httpd 配置

# managed by uberspace-generate-httpd-config

<Directory /var/www/virtual/USERNAME>
AllowOverride AuthConfig FileInfo Indexes Limit Options=ExecCGI,Includes,Indexes,MultiViews,SymLinksIfOwnerMatch
Options +Includes
</Directory>

<VirtualHost *>
ServerName USERNAME.uber.space


ServerAlias blog.returnnull.de


ServerAlias www.returnnull.de


ServerAlias USERNAME.uber.space


ServerAlias returnnull.de



SuexecUserGroup USERNAME USERNAME
DocumentRoot /var/www/virtual/USERNAME/html

ServerAdmin USERNAME@uber.space

AllowEncodedSlashes NoDecode


ErrorLog /readonly/USERNAME/logs/error_log_apache
LogLevel notice


RewriteEngine On

# If there is a host-specific pseudo-DocumentRoot, use it instead of the default one
RewriteCond /var/www/virtual/USERNAME/%{HTTP_HOST} -d
RewriteRule (.*) /var/www/virtual/USERNAME/%{HTTP_HOST}

<FilesMatch "\.php$">
    SetHandler  "proxy:unix:/run/php-fpm-USERNAME.sock|fcgi://php-fpm-USERNAME"
</FilesMatch>

<Proxy "fcgi://php-fpm-USERNAME" max=10>
</Proxy>

</VirtualHost>

您必须在项目的根目录中添加一个 .htaccess 文件。

RewriteBase /
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

为此,您需要为 apache 启用重写模块,您可以通过 运行 sudo a2enmod rewrite 执行此操作,此后您需要重新启动 apache 服务器 sudo service apache2 restart