Wsl ubuntu virtualhost and hosts file on windows

I try to access it through my windows browser and fail. what is my mistake?

I use WSL2 on windows 10, with ubuntu 20.
I have an apache server and a laravel app which I installed at /var/www/law
In my windows hosts file I've added this:

127.0.0.1   law.dev
::1 law.dev

and this is the law.conf file which is located at /etc/apache2/sites-available.

<VirtualHost *:80>
    ServerName law.dev


    DocumentRoot /var/www/law/public

    <Directory /var/www/law>
    Options Indexes MultiViews
    AllowOverride None
    Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Your WSL2 linux system has its own ip address.

You can get its current ip address with this command (on your windows host, with PowerShell or CMD.EXE): wsl hostname -I

You can now use this ip address in your hosts file (on windows host).

The ip address of your WSL2 system is not static so if you restart your pc your WSL2 host will have a different ip address.

Have a look at this ticket for more informations: https://github.com/microsoft/WSL/issues/4210

If you want to access your apache webserver on localhost you have to use WSL1 which uses the interface of the windows host.

You can convert your WSL2 linux system to WSL1 with this command ("Ubuntu" might differ on your system):

wsl --set-version Ubuntu 1

To get the name of your WSL2 system:

wsl --list --verbose

Output (Example):

  NAME      STATE           VERSION
* Ubuntu    Running         2