Score:0

Apache how to find cause of 404 with rewriterule ignored

in flag
Tom

So I have this

RewriteCond %{REQUEST_URI} (da-dk)/(.*)(/|\.php)$
RewriteRule ^(da-dk)/(.*)(/|\.php)$ /$2$3 [L]

But for some reasons example.com/da-dk/ gives "404 - not found"

I would like to try understand why apache reaches that conclusion - i.e. what rules it encountered and followed

kz flag
This rule is presumably in the root `.htaccess` file? Do you have any other rules? How do you expect a request for `/da-dk/` to be processed?
Score:1
kz flag

Assuming this is in the root .htaccess file...

RewriteRule ^(da-dk)/(.*)(/|\.php)$ /$2$3 [L]

A request for /da-dk/ (which matches against the URL-path da-dk/) does not match the regex ^(da-dk)/(.*)(/|\.php)$ so this rule is not processed/followed.

So, unless you have other rules that match this request then no rules are followed, hence the 404. (As for what rules are "encountered", well, all the other rules you have are encountered.)

Incidentally, the preceding condition (RewriteCond directive) is entirely superfluous here and should be removed. It's not doing anything more that what the RewriteRule pattern has already established.

Specifically, this rule (regex) would match URLs of the form:

  • /da-dk/.php
  • /da-dk/<foo>/
  • /da-dk/<foo>.php
  • /da-dk/<foo>/<bar>/<baz>/
  • /da-dk/<foo>/<bar>/<baz>.php

It does not match:

  • /da-dk/<foo>
  • /da-dk/

This rule simply removes the /da-dk prefix on the requested URL (presumably in the hope that the rewritten URL matches a filesystem directory or file).

If /da-dk/ should be rewritten back to the root then the above rule can be modified to allow for this (in the process, it can also be simplified slightly). For example, try the following instead:

RewriteRule ^da-dk/(.+(/|\.php))?$ /$1 [L]

Here, the part of the URL-path after da-dk/ is made optional. (In this case, the $1 backreference is effectively empty.)

By changing the quantifier from * to + this also avoids matching the (nonsense) URL /da-dk/.php.

No need to capture the da-dk part (first path segment), since this is not being used in the substitution string (2nd argument).

The $1 backreference now contains the entire URL-path after the da-dk/ prefix. (No need for $2 and $3 as you had originally.)

Tom avatar
in flag
Tom
I think I will mark this an answer and maybe create a new SO. I am still getting 404. (I had tried some different rewriterules and always got 404. But your rewriterule is better then those I tried. So under all circumstances I had a bug there.) I had hoped for Apache logging that could tell me at what lines rules were followed.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.