You are not logged in.

#1 2022-06-13 15:51:04

cadmium
Member
Registered: 2022-06-13
Posts: 6

Can`t set http headers in .htaccess

Hi,

I`m using a test environment with apache http server and php for web development.
I have serveral virtual hosts, each with its own config file as described here.
All website files are located in /srv/http in their own subdirectory.
In /etc/httpd/conf/httpd.conf I have following directory settings:

...
DocumentRoot "/srv/http"
<Directory "/srv/http">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>
...

I can verify that the .htaccess files are being read successfully, but If I try to add a http response header in .htaccess like so

<IfModule mod_headers.c>
    Header set Test-Header "test"
</IfModule>

the header is not being transferred (I have cache disabled in my browser).

If I set the header in the virtual host files it works fine.

I know that the AllowOverride option is needed.


Maybe someone has an idea what I`m missing here?

Thanks

Offline

#2 2022-06-13 16:23:13

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: Can`t set http headers in .htaccess

Why do you want to use htaccess??  If you can edit the server config, use that.

But in you really must use an htaccess, you need to write it as an htaccess: these are *not* xml files and do not follow the same format as apache cofnigs.    There has been (at least previously) a "Header" directive that can be used in htaccess files, e.g.:

Header add Test-Header "test"

EDIT: I suppose apache conf files aren't actually XML either, but they use some xml-like section tags.  And apparently htaccess files can also use some xml-like tags to subset what files commands apply to.  But just the same, htaccess files and apache config files use different formats / directives - you can't just move content between the two and expect it to work.

Last edited by Trilby (2022-06-13 16:28:27)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2022-06-13 18:05:28

cadmium
Member
Registered: 2022-06-13
Posts: 6

Re: Can`t set http headers in .htaccess

Thanks for the clarification.

I have a web project that is hosted by a provider where I don`t have access to main server configuration.
So the only way setting individual headers is using .htaccess or php.

The interesting thing is that setting the headers via .htaccess on the "productive" server is working just fine.
And the same file on my test system is not working.
Therefore I think there is something wrong with my local configuration.

Furthermore I don`t see any errors in the log files.

Offline

#4 2022-10-19 09:00:26

cadmium
Member
Registered: 2022-06-13
Posts: 6

Re: Can`t set http headers in .htaccess

As my old laptop got broken I did an installation of archlinux on a new machine.
Now the problem is gone.
Response headers are being set from .htaccess.

Offline

#5 2022-10-19 12:57:46

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: Can`t set http headers in .htaccess

Note that apache itself can be configured to use or ignore htaccess files.  Generally the admins of shared hosting services enable them as it's expected that users will need to use them.  I don't know what the defaults on arch's apache package currently are, but if you have access to the server config itself, it's best to disable htaccess files.  The notable exception here is if you are testing content for a shared hosting system on your own arch server (so you replicate similar server configs).


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

Board footer

Powered by FluxBB