- Instant help with your Developer coding problems

Redirect www to non-www on Apache webservers

Question:
How to redirect www to non-www on Apache webservers?
Answer:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
</IfModule>
Description:

To automatically redirect all requests from www to a non-www version in a generic way you can use the mod_rewrite module. Insert the code in your htaccess file and all your www requests will be redirected by Apache to a non-www version.

Share "How to redirect www to non-www on Apache webservers?"