Redirect users with rewrite rules when using hosts file to map subdomains to localhost on Apache server

Solution 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>