apache redirect from non www to www

前端 未结 24 915
春和景丽
春和景丽 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:12

    -If you host multiple domain names (Optional)

    -If all those domain names are using https (as they should)

    -if you want all those domain names to use www dot domainName

    This will avoid doble redirection (http://non www to http://www and then to https://www)

    <VirtualHost *:80>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^(.*)$ https://www.%1$1 [R=301,L]
    </VirtualHost>
    

    And

    <VirtualHost *:443>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}$1 [R=301,L]
    </VirtualHost>
    

    You should change the redirection code 301 to the most convenient one

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

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

    check this perfect work

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

    I've just have a same problem. But solved with this

    RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

    This rule redirecting non-www to www.

    And this rule to redirecting www to non-www

    RewriteEngine On RewriteCond %{HTTP_HOST} !^my-domain\.com$ [NC] RewriteRule ^(.*)$ http://my-domain.com/$1 [R=301,L]

    Refer from http://dense13.com/blog/2008/02/27/redirecting-non-www-to-www-with-htaccess/

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

    If using the above solution of two <VirtualHost *:80> blocks with different ServerNames...

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

    ... then you must set NameVirtualHost On as well.

    If you don't do this, Apache doesn't allow itself to use the different ServerNames to distinguish the blocks, so you get this error message:

    [warn] _default_ VirtualHost overlap on port 80, the first has precedence
    

    ...and either no redirection happens, or you have an infinite redirection loop, depending on which block you put first.

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

    Redirect domain.tld to www.

    The following lines can be added either in Apache directives or in .htaccess file:

    RewriteEngine on    
    RewriteCond %{HTTP_HOST} .
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    • Other sudomains are still working.
    • No need to adjust the lines. just copy/paste them at the right place.

    Don't forget to apply the apache changes if you modify the vhost.

    (based on the default Drupal7 .htaccess but should work in many cases)

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

    I had a similar task on a WP Multisite, where the redirection rule had to be generic (for any given domain I'd add to the network). I solved first adding a wildcard to the domain (parked domain). Note the . after .com.

    CNAME * domain.com.
    

    And then I added the following lines to the .htaccess file at the root of my multisite. I guess it'd work for any site.

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
    

    Hopefully this will help.

    ps. If you'd like to redirect from not www to www, change the last line into

    RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
    
    0 讨论(0)
提交回复
热议问题