You are not logged in.

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

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

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: 26,865
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: 2

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

Board footer

Powered by FluxBB