XAMPP managing headers (server response headers) - apache

I need to modify my localhost page server response (edit headers) - I'm using XAMPP (apache + msql, on linux machine). I can't find how to do that. Maybe some of the programmers/admins know how I can make it happened - share their knowledge and save me time.
If there is any article/link I would use it gladly.
I cannot use live server (my page is a total mess - it working on localhost by miracle...), and the only thing I have installed is xampp, so editing response headers through xampp would be perfect.
Thanks for any directions.
EDIT:
https://www.a2hosting.com/kb/developer-corner/apache-web-server/modifying-http-headers
is not working for me.
<IfModule mod_headers.c>
Header set Test "testing"
</IfModule>
inside of .htaccess

In you .htaccess file (witch should be placed where your index.html is) type:
### add custom header to all server responses from ALL files:
Header add Custom-Header: "parameter=value"
### add custom header to SINGLE file:
<Files someOtherFile.html>
Header add Custom-Header: "parameter=value"
</Files>
I'm using linux, maybe on windows there is something more you should do but remember to restart xampp after editing .htaccess.

Related

Enable CORS with wamp on windows 8

I have a cross domain request problem with an application I'm doing. I really spent hours looking for a solution on how to enable CORS with wamp (localhost) but nothing worked for me.
I have Apache 2.4.9 on Windows 8.1. I have enable the headers, I tried to put
Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"
in a .htaccess and in the http.conf as well as countless other variations.
If anyone had a solution that'd be awesome!
I had the same problem and i solved it with these 3 steps:
1) in Apache config file (for me the path was C:\wamp\bin\apache\apache2.4.18\conf\httpd.conf)
add the line:
Header set Access-Control-Allow-Origin "*"
in the content of the <Directory> tag:
DocumentRoot "c:/wamp/www"
<Directory "c:/wamp/www/">
Options +Indexes +FollowSymLinks
Header set Access-Control-Allow-Origin "*"
AllowOverride all
Require local
</Directory>
2) activate the "headers_module" in apache's modules (it will also restart your apache server, effectively applying the change made in step 1)
3) clear your browser cache (I am using chrome and i was told the best way to "hard clear" the cache was to go in the developper tools -> Networks tab -> right click -> clear browser cache)
(by the way, clearing the browser cache is often useful when debugging in chrome)
Now it should work. Good luck !
You must also activate the Apache Headers module.
Using the wampmanager menus do this :-
wampmanager -> Apache -> Apache modules -> headers_module
Make sure this is ticked, if its not, click that menu item and wait a few seconds while WampServer restarts Apache.

cakephp setting custom header does not work, htaccess, apache

I am experiencing some odd behaviour.
I have debian 7(on the vmware if it matters) with apache 2.2.22. For my cakephp application I want to set custom header, so I put this in app/webroot/.htaccess file (without removing what already exists of course)
<IfModule mod_headers.c>
Header append X-FRAME-OPTIONS: DENY
</IfModule>
but when I request the page, in firebug net panel it does not show X-FRAME-OPTIONS header. Headers mod is enabled. a2enmod headers outputs Module headers already enabled. Apache is restarted (even OS is rebooted). This cakephp application(copied by 100%) I tested on my vps(again debian 7), and it shows that header just fine. So, first it made me believe there is smth wrong with my local debian, but then I tested this. I created single file index.php in www/some_test folder and put echo "ok". Also created .htaccess with the same content
<IfModule mod_headers.c>
Header append X-FRAME-OPTIONS: DENY
</IfModule>
And the funny part is, I could see in firebug that X-FRAME-OPTIONS header. So, the bottom line with cakephp application, in the server1 custom headers are fine, in server2 - does not show, for simple index.php 'app' in server2 headers are fine again. Can someone help what the problem can be. I need to set custom headers.
Thanks
I am not sure what was the problem, but removing and reinstalling the php solved it
apt-get remove php5*
apt-get install php5
https://superuser.com/questions/673837/php5-ini-file-is-blank/674408#674408

<FilesMatch> in apache 2.4

I am using apache 2.4 and I want to create a link on my website to download a picture.
According to what I have found on Google, I have to put the following lines :
<FilesMatch "\.(jpg)$">
Header set Content-Disposition attachment
</FilesMatch>
But I dont know in which file I have to put this, because what I have found is about .htaccess and httpd.conf files, but those dont exist on Apache 2.4.
Thank you
Option 1: Create a new DOCUMENT_ROOT/.htaccess file and put this code there.
Option 2: Otherwise place this code in your Apache config and restart your Apache server

PHP: How to code .htaccess to make it work both on localhost & online without editing

I have a .htaccess file & I currently I am working on localhost. For a 404 page error, I have the following code in the .htaccess file:
ErrorDocument 404 /my_local_domain/404.php
But when I upload this file to my website online, the functionality of the file breaks. It no longer shows the 404.php page. It works if I modify the code in the .htaccess file of my online website to the following:
ErrorDocument 404 /404.php
Now all through the changes that I do in the .htaccess file, I would have to remember to remove the domain name before I upload it to the website or I risk breaking the functionality. So with this in mind, here are my questions:
1. How do I solve the above problem without needing to edit the .htaccess file each time (by stripping it off the my_local_domain) I make a change & upload it online?
2. How do I setup 404 page redirection for all the nested folders? (I don't want to setup a .htaccess file for each of the folders. A single .htaccess file that resides in the root folder of the website & controls all the redirection for all the sub-folders would be awesome)
All help is appreciated.
Thank you.
I believe you have two different issues here.
First of all, you should not need to have different paths in development and live site. It appears that you've configured your local Apache to host only one site and each actual sites goes in a subdirectory. It's not a good idea: you'll soon be mixing cookies and sessions between all your dev sites. Have a look at the name based virtual hosts feature: you can configure as many independent sites as you need. You don't even have to buy real domains in you set them in the hosts file.
Secondly, under certain circumstances it can be useful to have different Apache directives. I've been using the following trick.
Pick a keyword for the dev server, e.g. DEV_BOX.
Pass that keyword to Apache in the -D parameter. If you run it as service, you can run regedit and find the HKLM\SYSTEM\CurrentControlSet\Services\Apache2.2\Parameters key. Append -D DEV_BOX to the ConfigArgs value. Restart Apache.
Now, you can use the <IfDefine> directive to set local directives:
-
#
# Common stuff
#
AddDefaultCharset UTF-8
#
# Local-only stuff
#
<IfDefine DEV_BOX>
Options +Indexes
</IfDefine>
#
# Live-only stuff
#
<IfDefine !DEV_BOX>
Options -Indexes
</IfDefine>
First of all I suggest you setup local domains for development. For example if you are developing a website which will go under www.example.com, you can setup a local.example.com in your HOSTS file. You'll do a VirtualHost setup in your apache and the .htaccess will then be the same.
Also, you can setup a build process (e.g via Ant) which will allow you to prepare and generate a zip file with the files which go on the live server. This build will feature the correct configuration files (db configs, mail servers, htaccess etc).

Using keep-alive feature in .htaccess

I want to use the keep-alive feature in Apache. How can I do this with my host (.htaccess file), and what are the best values for the parameters like KeepAliveTimeout?
If Keep-alive is turned on in the Apache configuration, all you need is just set an HTTP header Connection: keep-alive. E.g. add following lines to your .htaccess file:
<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>
You can't control keepalive behaviour in an .htaccess. Keepalives are a host-level feature, not one where different directories can behave differently depending on the per-directory htaccess info.
If you are on the kind of basic shared hosting that only gives you .htaccess to configure your sites, you can't change the keepalive settings. Presumably the hosting company will have set them appropriately, or just left them on the default settings, which are usually fine.
Yes Keep-alive behavior can be controlled in .htaccess file.
First check the server setting by printing $_SERVER and if
[HTTP_CONNECTION] => keep-alive
is there then you just have to include the setting in your .htaccess file.
Add the following line at the end of .htaccess file in your project's root directory.
<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>
If you have SSH access to your server you should edit the Apache config file. Use these settings as a starter:
KeepAlive: on
KeepAliveTimeout: 3 seconds
MaxKeepAliveRequests: 60
This should work for most basic server setups with average traffic. You can always tweak the settings to suit your own needs. See here for more detailed info about this: http://www.giftofspeed.com/enable-keep-alive/
If you don't have access to your server you should contact your host. Changing the keepalive settings on your own by editing the .htaccess file will probably don't work.
It very much depends on your site and the amount of traffic it receives. If a user comes to your site, then clicks through to another page within the KeepAliveTimeout setting (default is 15), a new TCP does not have to be created. This can really help with overhead.
On the other hand, any Apache processes that are currently tied up w/ existing visitors will not be able to talk to the new ones. So you may have to increase the total number of Apache processes that are available.
In short... it requires tweaking.
you can't control keep-alive behavior in .htaccess
Paste the following code in your .htaccess file:
<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>
Then use this website: https://varvy.com/pagespeed/ to check if it's enabled.