Htaccess redirect www to non-www not working

Disclaimer: of course I have read and tried solutions suggested here and at stackoverflow. It’s not my first time trying to achieve this behaviour and in other systems I was successful, although not with Kirby. I almost get crazy about this issue :wink:

I simply want to redirect all www-subdomain requests to a non-www site.

So far, https://aschlemmer.net or Referenzen | Erstklassiges Webdesign aus Freiburg, a/schlemmer e/consult are working as intended.
But: https://www.aschlemmer.net results in a 404 error and I can’t figure out why. Has anybody an idea what could be my mistake? Note: website is hosted by https://df.eu where I can handle similar questions with no problems in WordPress installations.

My .htaccess file is a little long but I will paste the full one:

# Kirby .htaccess

# rewrite rules
<IfModule mod_rewrite.c>

# enable awesome urls. i.e.:
# http://yourdomain.com/about-us/team
RewriteEngine on

# make sure to set the RewriteBase correctly
# if you are running the site in a subfolder.
# Otherwise links or the entire site will break.
#
# If your homepage is http://yourdomain.com/mysite
# Set the RewriteBase to:
#
# RewriteBase /development

# In some environments it's necessary to
# set the RewriteBase to:
#
RewriteBase /

# block files and folders beginning with a dot, such as .git
# except for the .well-known folder, which is used for Let's Encrypt and security.txt
RewriteRule (^|/)\.(?!well-known\/) index.php [L]

# block text files in the content folder from being accessed directly
RewriteRule ^content/(.*)\.(txt|md|mdown)$ index.php [L]

# block all files in the site folder from being accessed directly
# except for requests to plugin assets files
RewriteRule ^site/(.*) index.php [L]

# Enable authentication header
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1

# block direct access to kirby and the panel sources
RewriteRule ^kirby/(.*) index.php [L]

# make site links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [L]

# https-Rewrite
# force entire site to ssl
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

AddType application/x-httpd-php .php3 .php .php4 .html
AddType image/x-icon .ico
AddDefaultCharset UTF-8

#### security

<IfModule mod_headers.c>
# HSTS
Header set Strict-Transport-Security "max-age=15558000;"
# div, siehe https://kb.sucuri.net/warnings/hardening/headers-x-frame-clickjacking
Header set X-Content-Type-Options nosniff
Header always append X-Frame-Options SAMEORIGIN
Header set X-XSS-Protection "1; mode=block"

</IfModule>

#Protect the htaccess file
<Files .htaccess>
Order Allow,Deny
Deny from all
</Files>

AddType video/ogg .ogv
AddType video/mp4 .mp4
AddType video/webm .webm

# 480 weeks
<filesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf|webp)$">
Header set Cache-Control "max-age=290304000, public"
</filesMatch>
 
# 2 DAYS
<filesMatch "\.(xml|txt)$">
Header set Cache-Control "max-age=172800, public, must-revalidate"
</filesMatch>
 
# 2 HOURS
<filesMatch "\.(html|htm)$">
 Header set Cache-Control "max-age=7200, must-revalidate"
</filesMatch>

<IfModule mod_headers.c>
# No ETags, No Pragma
Header unset Pragma
Header unset ETag
# Make sure proxies deliver correct content
Header append Vary User-Agent env=!dont-vary
# Ensure proxies deliver compressed content correctly
Header append Vary Accept-Encoding
</IfModule>

<IfModule mod_deflate.c>
<FilesMatch "\.(js|css|text|html)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>

Try putting your https//www rewrites before the Kirby specific ones.

thanks for your answer, pixeljn, I tried as you suggested but no effect …

Is caching enabled? Then disable it.

which cache you mean? Browser cache emptied before retrying.

I meant Kirby’s page cache

no Kirby caching activated in the site’s config.php

The problem might have a different root cause. For me, no DNS is resolved for www.aschlemmer.net:

Auflösen des Hostnamens »www.aschlemmer.net (www.aschlemmer.net)« … fehlgeschlagen: Zu diesem Hostnamen gehört keine Adresse.
wget: Host-Adresse »www.aschlemmer.net« kann nicht aufgelöst werden

which is quite disturbing for me and I asked the hosting company – glad that they could help: first, it was an A-record missing which they added and second they changed the htaccess entry to:

RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} ^www. [NC]
RewriteRule (.*) https://aschlemmer.net%{REQUEST_URI} [L,R=301]

Thanks to all of you! Solved.

1 Like