Redirect non-www to www in .htaccess

后端 未结 13 1674
轻奢々
轻奢々 2020-11-22 05:51

I have this in my .htaccess file:

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

but whenever I

相关标签:
13条回答
  • 2020-11-22 06:20

    This configuration worked for me in bitnami wordpress with SSL configured :

    Added the below under "RewriteEngine On" in file /opt/bitnami/apps/wordpress/conf/httpd-app.conf

    RewriteCond %{HTTP_HOST} .
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    RewriteBase /
    RewriteCond %{HTTPS} !on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    0 讨论(0)
  • 2020-11-22 06:24

    Change your configuration to this (add a slash):

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

    Or the solution outlined below (proposed by @absiddiqueLive) will work for any domain:

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

    If you need to support http and https and preserve the protocol choice try the following:

    RewriteRule ^login\$ https://www.%{HTTP_HOST}/login [R=301,L]
    

    Where you replace login with checkout.php or whatever URL you need to support HTTPS on.

    I'd argue this is a bad idea though. For the reasoning please read this answer.

    0 讨论(0)
  • 2020-11-22 06:24

    Try this, I used it in many websites, it works perfectly

    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^bewebdeveloper.com$
    RewriteRule ^(.*) http://www.bewebdeveloper.com/$1  [QSA,L,R=301]
    
    0 讨论(0)
  • 2020-11-22 06:27

    If possible, add this to the main Apache configuration file. It is a lighter-weight solution, less processing required.

    <VirtualHost 64.65.66.67>
            ServerName example.com
            Redirect permanent / http://www.example.com/
    </VirtualHost>
    <VirtualHost 64.65.66.67>
       ServerAdmin me@example.com
       ServerName www.example.com
       DocumentRoot /var/www/example
       .
       .
       . etc
    

    So, the separate VirtualHost for "example.com" captures those requests and then permanently redirects them to your main VirtualHost. So there's no REGEX parsing with every request, and your client browsers will cache the redirect so they'll never (or rarely) request the "wrong" url again, saving you on server load.

    Note, the trailing slash in Redirect permanent / http://www.example.com/. Without it, a redirect from example.com/asdf would redirect to http://www.example.comasdf instead of http://www.example.com/asdf.

    0 讨论(0)
  • 2020-11-22 06:30

    Here's the correct solution which supports https and http:

    # Redirect to www
    RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
    RewriteCond %{HTTPS}s ^on(s)|
    RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    0 讨论(0)
  • 2020-11-22 06:33

    Write in .htaccess :)

    ## Redirect from non-www to www (remove the two lines below to enable)
        RewriteCond %{HTTP_HOST} !^www\. [NC]
        RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
    
    0 讨论(0)
提交回复
热议问题