larsb-dev's avatar

Docker Container Routing not working [SOLVED myself]

Hello In the video "Make A Router" https://laracasts.com/series/php-for-beginners-2023-edition/episodes/15 I struggle to get the routing to work for the URLs other than "/". I get

Not Found
The requested URL was not found on this server.

Does this have sth to do with apache?

My docker compose

version: "3.8"

services:
  php:
    image: php:8.2-apache
    ports:
      - "8080:80"
    volumes:
      - ./:/var/www/html
0 likes
3 replies
larsb-dev's avatar

After almost 1 hour of trying I asked GPT and found out that I need .htaccess if I use Apache

RewriteEngine On
RewriteBase /

# If the request is not for a valid file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other requests to index.php
RewriteRule ^ index.php [L]

also, mod_rewrite needs to be enabled as well as .htaccess allowed to override settings. Therefore, I created a Dockerfile

FROM php:8.2-apache

# Enable Apache mod_rewrite
RUN a2enmod rewrite

# Allow .htaccess to override settings
RUN sed -i 's|AllowOverride None|AllowOverride All|' /etc/apache2/apache2.conf

The new docker compose file is now using the Dockerfile to build the image

services:
  php:
    build: .
    ports:
      - "8080:80"
    volumes:
      - ./:/var/www/html

docker compose up -d --build

Glukinho's avatar

Print $_SERVER['REQUEST_URI'] at the end if your index.php and you will see what is actually comes to this variable, after that you'll know what is the problem:

print_r($_SERVER['REQUEST_URI']);

die();
larsb-dev's avatar

The problem was that a2enmod rewrite wasn't enabled inside the apache image. It had nothing to do with my router or PHP code.

Please or to participate in this conversation.