apache redirect from non www to www

前端 未结 24 914
春和景丽
春和景丽 2020-11-22 06:34

I have a website that doesn\'t seem to redirect from non-www to www.

My Apache configuration is as follows:

RewriteEngine On
### re-direct t         


        
相关标签:
24条回答
  • 2020-11-22 07:00

    Do not always use Redirect permanent (or why it may cause issues in future)

    If there is a chance that you will add subdomains later, do not use redirect permanent.

    Because if a client has used a subdomain that wasn't registred as VirtualHost he may also never reach this subdomain even when it is registred later.

    redirect permanent sends an HTTP 301 Moved Permanently to the client (browser) and a lot of them cache this response for ever (until cache is cleared [manually]). So using that subdomain will always autoredirect to www.*** without requesting the server again.

    see How long do browsers cache HTTP 301s?

    So just use Redirect

    <VirtualHost *:80>
      ServerName example.com
    
      Redirect / http://www.example.com/
    </VirtualHost>
    

    Apache.org - When not to use mod_rewrite

    Apache.org - Canonical Hostnames

    0 讨论(0)
  • 2020-11-22 07:01

    This works for me:

    RewriteCond %{HTTP_HOST} ^(?!www.domain.com).*$ [NC]
    RewriteRule ^(.*)$  http://www.domain.com$1  [R=301,L]
    

    I use the look-ahead pattern (?!www.domain.com) to exclude the www subdomain when redirecting all domains to the www subdomain in order to avoid an infinite redirect loop in Apache.

    0 讨论(0)
  • 2020-11-22 07:01

    The code I use is:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
    
    0 讨论(0)
  • 2020-11-22 07:03
    <VirtualHost *:80>
        ServerAlias example.com
        RedirectMatch permanent ^/(.*) http://www.example.com/$1
    </VirtualHost>
    

    This will redirect not only the domain name but also the inner pages.like...

    example.com/abcd.html               ==>    www.example.com/abcd.html
    example.com/ab/cd.html?ef=gh   ==>    www.example.com/ab/cd.html?ef=gh

    0 讨论(0)
  • 2020-11-22 07:04

    Using the rewrite engine is a pretty heavyweight way to solve this problem. Here is a simpler solution:

    <VirtualHost *:80>
        ServerName example.com
        Redirect permanent / http://www.example.com/
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerName www.example.com
        # real server configuration
    </VirtualHost>
    

    And then you'll have another <VirtualHost> section with ServerName www.example.com for your real server configuration. Apache automatically preserves anything after the / when using the Redirect directive, which is a common misconception about why this method won't work (when in fact it does).

    0 讨论(0)
  • 2020-11-22 07:08
        <VirtualHost *:80>
           DocumentRoot "what/ever/root/to/source"
           ServerName www.example.com
    
           <Directory "what/ever/root/to/source">
             Options FollowSymLinks MultiViews Includes ExecCGI
             AllowOverride All
             Order allow,deny
             allow from all
             <What Ever Rules You Need.>
          </Directory>
    
        </VirtualHost>
    
        <VirtualHost *:80>
          ServerName example.com
          ServerAlias *.example.com
          Redirect permanent / http://www.example.com/
        </VirtualHost>
    

    This is what happens with the code above. The first virtual host block checks if the request is www.example.com and runs your website in that directory.

    Failing which, it comes to the second virtual host section. Here anything other than www.example.com is redirected to www.example.com.

    The order here matters. If you add the second virtualhost directive first, it will cause a redirect loop.

    This solution will redirect any request to your domain, to www.yourdomain.com.

    Cheers!

    0 讨论(0)
提交回复
热议问题