Question: Example of a file

```
line 1
line 2
line 3
line 4

```

`line 4` should be after `line 1`

```
line 1
line 4
line 2
line 3

```

Important: It could be more than 4 lines somehow the script has to understand that it has to read the last line, **not** line 4.

Answer: 
First, there is an issue with your `.htaccess` file:

* In lines 6-8; you require that the user is authenticated, but only if it's not an `OPTIONS` request. This is fine.
* In line 4 however, you require that the user is authenticated as user `usernam`, regardles of the request method (GET, POST, OPTIONS, etc...)

So if you remove line 4 or move it into the `LimitExcept` section your config should work.

For more information see the [mod\_authz\_core docs](https://httpd.apache.org/docs/current/mod/mod_authz_core.html)

---

Second, the error message for the first solution you posted ("The server encountered an internal error or misconfiguration...") hints at an invalid `httpd.conf` file. There may be something else misconfigured. Check your configuration and the [Apache documentation](https://httpd.apache.org/docs/current/).

---

As a reference, the config files I used for testing can be found at: <https://github.com/mhutter/stackexchange/tree/master/467654>
The error `401 Unauthorized` means that the HTTP server expects credentials, usually a user name and password. Accoring to the question, you removed the `.htacccess` file that requested authentication, so there must be still something in the active configuration that requests authentication.

To provide the credentials, with `curl` you can use the option `-u, --user <user:password>` (short or long form).

---

When you get an error `500`, there should be an entry in the Apache logs that explains what king of error occurred.

---

When something doesn't work, it is generally a good idea to reduce the configuration to the required minimum to find the problem. To do that, comment out lines you think don't cause the problem and try again. Either it starts working, then you know where to look at more closely, or you have a simplified config.

With your config, the header lines probably don't contribute to the problem, but I would first try to get it working without the redirect. When you have a working config, you can add lines until either it breaks again or you have the config you want.