1

I would like to redirect www and non-www with a subdomain to non-www HTTPS.

I think this should clarify.

<VirtualHost *:80>
    DocumentRoot "C:/www/eshop"
    ServerName shop.example.com
    ServerAlias www.shop.example.com
    
    # shop.example.com should redirect to https://shop.example.com
    # www.shop.example.com should redirect to https://shop.example.com
</VirtualHost>

<VirtualHost *:443>
    DocumentRoot "C:/www/eshop"
    ServerName shop.example.com
    ServerAlias www.shop.example.com

    # I would like to redirect www and non-www to https://shop.example.com
    
    # https://shop.example.com should redirect to https://shop.example.com
    # https://www.shop.example.com should redirect to https://shop.example.com

    <Directory "C:/www/eshop">
        # some code will go here
    </Directory>
    
</VirtualHost>
Roland
  • 13
  • 2
  • Does this answer your question? [Redirect URL within Apache VirtualHost?](https://serverfault.com/questions/120488/redirect-url-within-apache-virtualhost) – HBruijn Aug 29 '22 at 09:13

1 Answers1

0

Use the Redirect directive for simple redirects:

<VirtualHost *:80>
    ServerName shop.example.com
    ServerAlias www.shop.example.com
    Redirect "/" "https://shop.example.com/"
</VirtualHost>

<VirtualHost *:443>
    ServerName www.shop.example.com
    Redirect "/" "https://shop.example.com/"
    # ... Maybe some SSL configuration is needed here
</VirtualHost>

<VirtualHost *:443>
    DocumentRoot "C:/www/eshop"
    ServerName shop.example.com
    # ... Maybe some SSL configuration is needed here

    <Directory "C:/www/eshop">
        # some code will go here
    </Directory>
    
</VirtualHost>
HBruijn
  • 72,524
  • 21
  • 127
  • 192
  • Redirect for shop.example.com works fine, but www.shop.example.com does not work. I get 'This site can’t be reached' message. – Roland Aug 29 '22 at 13:09
  • That is probably unrelated to your web server configuration and most likely caused elsewhere. (for example you such an error when the DNS record for www.shop.example.com does not exist.) – HBruijn Aug 29 '22 at 13:12
  • @HBrujin you were right, there was a problem with DNS – Roland Sep 21 '22 at 07:00