How to dynamically redirect www-based URLs to non-www URLs with multiple domains in same VirtualHost

后端 未结 3 931
轮回少年
轮回少年 2021-02-06 06:39

I\'ve got a VirtualHost that looks something like:



  ServerName  domain1.com
  ServerAlias www.domain1.com domain2.com www.domain2.com
         


        
相关标签:
3条回答
  • 2021-02-06 07:26

    You can have multiple VirtualHosts in a configuration file, so you should change your config to this:

    <VirtualHost *:80>
        ServerName domain1.com
        ServerAlias www.domain1.com
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerName domain2.com
        ServerAlias www.domain2.com
    </VirtualHost>
    

    You can add another VirtualHost for each domain you want to do.

    0 讨论(0)
  • 2021-02-06 07:40

    No need for rewrites.

    <VirtualHost *:80>
        ServerName domain1.com
        ServerAlias domain2.com
        ... real vhost settings ...
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerName www.domain1.com
        Redirect permanent / http://domain1.com/
    </VirtualHost>
    <VirtualHost *:80>
        ServerName www.domain2.com
        Redirect permanent / http://domain2.com/
    </VirtualHost>
    
    0 讨论(0)
  • 2021-02-06 07:43

    Your RewriteCond is a bit wonky. I'm surprised it does anything at all, since it would seem to be trying to match the host www.domain1.com against the pattern www\.www.domain1.com. These directives worked for me:

    # Redirect www to non-www
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^(.*)$ http://%1$1 [L,R=301]
    
    0 讨论(0)
提交回复
热议问题