I'm getting a 404 when typing my domain in the address bar without www in front of it. Only Safari on a Mac seems to work fine, but Chrome and Firefox get a 404 not found. Why is that, and how to I change it?
domain.nl
works in Safari but returns 404 in Chrome/Firefox/etc.
www.domain.nl
works in all browsers.
The non-www version does not automatically change to the www version in Chrome/Firefox/etc.
RewriteEngine On
# AUTO HTTP TO HTTPS
RewriteCond %{HTTP_HOST} ^domain\.nl$
RewriteRule ^(.*) http://www.domain.nl/$1 [R=301]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php?_route_=$1 [L,QSA]
# FOR NON-WWW QUERIES
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [R=301,L]
Please note: I have replaced the actual domain with the text "domain" in the code.
# AUTO HTTP TO HTTPS RewriteCond %{HTTP_HOST} ^domain\.nl$ RewriteRule ^(.*) http://www.domain.nl/$1 [R=301]
You have a couple of "errors" in your first rule that redirects non-www to www, which are going to cause problems:
You are missing the L
flag on the rule so processing is going to continue and possibly be rewritten, resulting in a malformed redirect (that could result in a 404).
You are redirecting to HTTP, not HTTPS.
The first rule should read:
RewriteCond %{HTTP_HOST} ^domain\.nl
RewriteRule (.*) https://www.domain.nl/$1 [R=301,L]
# FOR NON-WWW QUERIES RewriteCond %{HTTP_HOST} !^www\. RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [R=301,L]
Your last rule is superfluous and can be deleted, since this is what the first rule does! Except this rule is rather more general and does not explicitly include the domain name.
Alternatively, you replace the first rule with this one. However, this rule doesn't necessarily work if you have other subdomains, unless you want all subdomains to also have a www sub-subdomain?
You will need to clear your browser cache before testing, since any erroneous 301 (permanent) redirect will have been cached by the browser. (Test first with 302 - temporary - redirects to avoid potential caching issues.)
Caching might explain the difference in browser behaviour you are seeing?
Aside:
RewriteRule ^(.*)\?*$ index.php?_route_=$1 [L,QSA]
The regex you are using here is a bit "odd" and probably isn't doing what you think it's doing. The regex ^(.*)\?*$
is the same as simply (.*)
because .*
is greedy and \?*
is effectively optional (matching 0 or more times), so .*
consumes (and (.*)
captures) the entire URL-path anyway.
The presence of \?*$
at the end of the regex would seem to suggest you are expecting %3F
(URL encoded ?
) at the end of the URL-path. However, the regex as stated does not exclude these from the capturing group.