docker-compose - php - .htaccess - 500 Internal Server Error

Viewed 951

I try to setup a PHP (vanilla) project with MVC architecture and docker-compose.

For my docker web service i use : php:7.2.2-apache

So it's look like:

enter image description here

This is my docker-compose:

version: '3'

services:
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: pass
      MYSQL_DATABASE: db
    ports:
      - "9906:3306"

  web:
    build:
      dockerfile: php.Dockerfile
      context: .
    container_name: php_web
    depends_on:
      - db
    volumes:
      - ./:/var/www/html/
    ports:
      - "8080:80"
    stdin_open: true
    tty: true

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    depends_on:
      - db
    external_links:
      - db:mysql
    ports:
      - "9191:80"
    environment:
      MYSQL_USER: root
      MYSQL_PASSWORD: pass
      MYSQL_ROOT_PASSWORD: pass
      PMA_HOST: db

and my .htaccess

RewriteEngine On
RewriteRule .* index.php

Without .htaccess i can go to localhost and see my index.php, but with .htaccess i have a 500 Internal Server Error

How can i properly configure my project ? I try to follow this -> MVC - Stackoverflow and MVC - Github

1 Answers
RewriteRule .* index.php

This rewrites everything (including any other static resources and index.php itself) to index.php. This will certainly break your app if you have any other static resources, although it shouldn't necessarily cause a rewrite loop (500 error).

Try the following instead:

DirectoryIndex index.php

RewriteEngine On

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

The mod_rewrite directives don't actually do anything when you request the document root (the DirectoryIndex is served), but for any non-empty URL-path this applies.

However, this is still dependent on .htaccess overrides and mod_rewrite being enabled. (If mod_rewrite is not enabled then that could also be the cause of the 500 error - see the server's error log for details of the error.)

Related