Skip to content

Instantly share code, notes, and snippets.

@BenceSzalai
Created December 10, 2023 11:55
Show Gist options
  • Save BenceSzalai/0277c9cbe5b3f905b6bbc755c667e7a2 to your computer and use it in GitHub Desktop.
Save BenceSzalai/0277c9cbe5b3f905b6bbc755c667e7a2 to your computer and use it in GitHub Desktop.
Redirect www/non-www variants
# Ensure canonical domain
<IfModule mod_rewrite.c>
RewriteEngine On
# Use one of these two:
# Rewrite www.domain.com -> domain.com
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# OR:
# Rewrite domain.com -> www.domain.com
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} (.+)$
RewriteRule ^(.*)$ https://www.%1/$1 [R=301,L]
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment