- Enable mod_rewrite: Add the following line to the top of your
.htaccess
file:
RewriteEngine On
- Exclude www for both HTTP and HTTPS requests: Add the following code block to redirect all requests (HTTP and HTTPS) to the non-www version of the URL:
RewriteCond %{HTTP_HOST} =www\.(.*) [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
Explanation of the code:
RewriteEngine On
: Enables the mod_rewrite module, which is required for URL rewriting.RewriteCond %{HTTP_HOST} =www\.(.*)
: Checks if the current request includes awww
prefix.[NC]
: Indicates that thewww\.
pattern should be matched case-insensitively.RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
: Redirects all requests, including HTTP and HTTPS, with awww
prefix to the corresponding non-www URL using a permanent 301 redirect. TheR=301
flag tells Apache to use a permanent redirect, and theL
flag tells Apache to stop processing the rewrite rules after this rule is matched.
Leave a Reply