Search code examples
phpdocker.htaccess

.htaccess file not working in docker container


I have a PHP project running on Docker. Whenever I include the htaccess file in the project, I get the error "500 Internal Server Error".

docker-compose.yml:

version: '3'
services:
  db:
    image: mysql:8.0.31
    environment:
      MYSQL_DATABASE: demo
      MYSQL_USER: admin
      MYSQL_PASSWORD: password
      MYSQL_ALLOW_EMPTY_PASSWORD: 1
    volumes:
      - "./db:/docker-entrypoint-initdb.d"
    networks:
      - lamp-docker
  www:
    depends_on:
      - db
    #image: php:7.4.30-apache
    volumes:
      - "./:/var/www/html"
    ports:
      - 80:80
      - 443:443
    build: .
    networks:
      - lamp-docker
  phpmyadmin:
    depends_on:
      - db
    image: phpmyadmin/phpmyadmin:4.9.7
    ports:
      - 8001:80
    environment:
      - PMA_HOST=db
      - PMA_PORT=3306
    networks:
      - lamp-docker
networks:
  lamp-docker:
    driver: bridge

Dockerfile:

FROM php:7.4.30-apache

RUN docker-php-ext-install pdo pdo_mysql
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli

.htaccess:

RewriteEngine On

RewriteRule ^([a-zA-Z0-9_-]+)--([a-zA-Z0-9_-]+)$ title.php?title=$1&title_id=$2

RewriteRule ^profile/([a-zA-Z0-9_-]+)$ profile.php?username=$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

None of the solutions I found from google worked. I hope you can help me.


Solution

  • You need to enable the mod_rewrite Apache module using a2enmod rewrite command in your docker configuration.