Search code examples
wordpressapache.htaccessovh

redirect www to non www in wordpress


I've added the non www url into my gerenal - setting - WordPress Address (URL) & Site Address (URL)

and edited my wordpress .htaccess as below. but it still doesn't redirect to the non www site, any idea why?

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /

# remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

</IfModule>

Solution

  • You can use below rewrite rule i.e

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

    If you don't want to use Mod rewrite rule then use redirect i.e

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