1

I am running an Apache 2.4 server in using docker-compose.

I have a "main" vhost which includes rewrites paths which start with /test/ to a test subdomain:

<VirtualHost *:80>
        ServerName example.com
        RewriteEngine on
        RewriteRule "^/test/(.*)$" "http://test.example.com/$1" [P]
</VirtualHost>

The test subdomain does not exist, and cannot be created (for business reasons which are irrelevant to the issue). The way I accomplish resolving test.example.com is to include hosts entries in the docker-compose.yml:

version: "3.7"

services:
    test:
        image: httpd:2.4
        ports:
            - 80:80
        extra_hosts:
            - "test.example.com:127.0.0.1"

This works, however in the vhost for test.example.com I have another rewrite rule:

<VirtualHost *:80>
        ServerName test.example.com
        RewriteEngine on
        RewriteCond %{REQUEST_URI} ^/login
        RewriteCond %{HTTP:COOKIE} ^.*cookie=([^;]+)
        RewriteRule /login /dashboard [R=302]
</VirtualHost>

This rule correctly rewrites http://test.example.com/login to http://test.example.com/dashboard on a successful login, but then the redirect happens in the user's browser and http://test.example.com cannot be resolved (ERR_NAME_NOT_RESOLVED).

How can I perform this rewrite in the test.example.com vhost so that it correctly redirects the user to the /dashboard page?

Employee
  • 113
  • 4

1 Answers1

1

You can add the redirect to the config for example.com:

<VirtualHost *:80>
    ServerName example.com
    RewriteEngine on

    RewriteCond %{HTTP:COOKIE} ^.*cookie=([^;]+)
    RewriteRule "^/test/login/?$" "/test/dashboard" [R=302]

    RewriteRule "^/test/(.*)" "http://test.example.com/$1" [P]
</VirtualHost>
Andrew Schulman
  • 8,561
  • 21
  • 31
  • 47