I have a website that uses the same core .htaccess details as many other websites; however this website does not properly load the .htaccess directives -- giving a basic HTTP header set of:
HTTP/1.1 200 OK
Date: Mon, 12 Nov 2018 09:34:28 GMT
Server: Apache
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
The website itself loads fine, but additonal headers in .htaccess are not being agknowledged / loaded.
So .htaccess is being read, right?
Yes -- The htaccess file contains HTTPS forced redirects and domain name redirects (from the .co.uk to .com address (both to the same website account))
These work.
Headers supplied by PHP are being loaded fine, too
The PHP headers on a test page are loading just fine:
<?php
header("Cache-Control: no-cache, must-revalidate");
header('Content-Type: text/html; charset=utf-8');
header("X-Clacks-Overhead: GNU Terry Pratchett");
header("Content-Language: en");
header("X-XSS-Protection: 1; mode=block");
header("X-Frame-Options: SAMEORIGIN");
header("X-Content-Type-Options: nosniff");
?>
But the same headers set in the .htaccess are not being agknowledged.
So it's an .htaccess syntax error!
Not that I can see; usually with a .htaccess error the site loads an HTTP-500 error message, however here the site loads in the browser without issue.
When there IS a deliberate syntax error the error-500 HTTP response comes back as expected.
Ok bozo, check your error logs!
Absolutely; I couldn't agree more. The Apache error logs are empty!
What have you tried to do to fix this?
Confirmed httpd.conf allows reading of .htaccess
Confirmed that mod_headers.c is loaded on the server
Commented out and re-written various rules, to no effect
Read lots (maybe 6-8) of posts on Stack Overflow and Server Fault - Stackoverflow posts don't appear to relate or their issues had distinct differences.
Confirmed my .htaccess has the correct permissins (0644)
Told my staff (He's a Graphic Designer).
Cried myself to sleep.
Right then - Get your file out! Show me the magic!
Here:
Options +FollowSymLinks
Options -Indexes
RewriteEngine On
ErrorDocument 404 /index.php?msg=404
ErrorDocument 403 /index.php?msg=403
#Set asset items to cache for 1 week.
<FilesMatch "\.(gif|jpe?g|png|ico|css|js|swf|mp3)$">
Header set Cache-Control "max-age=1972800, public, must-revalidate"
</FilesMatch>
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
## This does not appear to work (for either)
#Header always set Strict-Transport-Security "max-age=31536000;" env=HTTPS
Header always set Strict-Transport-Security "max-age=31536000; includeSubdomains;" "expr=%{HTTPS} == 'on'"
Header set Expect-CT enforce,max-age=2592000
RewriteCond %{HTTP_HOST} ^(www\.)?thewebsite\.co\.uk$ [NC]
RewriteRule ^/?(.*)$ https://www.thewebsite.com%{REQUEST_URI} [R=301,L]
###
##### Seems to workdown to roughly this point.
###
#force requests to begin with a slash.
RewriteCond %{REQUEST_URI} !^$
RewriteCond %{REQUEST_URI} !^/
RewriteRule .* - [R=403,L]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
### This file does not exist on the directory at present.
<Files .account-user.ini>
order allow,deny
deny from all
</Files>
###
#### None of these appear on assessment tools such as Security Headers
#### Or redbot.
###
Header set Cache-Control no-cache,must-revalidate
Header set X-Clacks-Overhead "GNU Terry Pratchett"
Header set X-XSS-Protection 1;mode=block
Header set X-Content-Type-Options nosniff
Header always set X-Frame-Options SAMEORIGIN
Header set Expect-CT enforce,max-age=2592000
Header set Content-Language en
Header set Referrer-Policy origin-when-cross-origin
<LimitExcept GET POST HEAD>
deny from all
</LimitExcept>
And finally it would really help if you gave me a final summary of all of the above!
Header setting commands in .htaccess do not appear to work.
ALL parts of the file are used on other live sites elsewhere without issue.
Headers can be set in PHP without issue
No errors arise from these Headers in the .htaccess.
Headers appear to fail silently.
No Apache error logs are recorded.
The .htaccess is being read by Apache because other commands (such as mod_Rewrites) are being actioned
UPDATE:
From research by other parties (the hosting providers) it seems that somehow the .htaccess works and loads all the correct headers for non PHP pages.
For even plain PHP pages; the headers are blank.
Clarification
whatever.html pages load the headers all ok.
PHP pages display headers set by Header("...");
PHP pages refuse to load any headers set by .htaccess. This is the problem.
So it looks like my .htaccess can't set headers for PHP pages. How can I fix this?
It seems that PHP ignores headers defined in .htaccess when working as a FastCGI module.
There are a lot of suggestions how to fix this. In your case I would recommend to have a file that defines all your headers
<?php
// file headers.php
header('Cache-Control: no-cache,must-revalidate');
header('X-Clacks-Overhead: "GNU Terry Pratchett"');
header('X-XSS-Protection: 1;mode=block');
header('X-Content-Type-Options: nosniff');
header('X-Frame-Options: SAMEORIGIN');
header('Expect-CT: enforce,max-age=2592000');
header('Content-Language: en');
header('Referrer-Policy: origin-when-cross-origin');
?>
and save it to your DocumentRoot directory. Then add this entry to your .htaccess file to include it with every request:
php_value auto_prepend_file /var/www/html/headers.php
Testing it:
<?php
// file test.php
die("hello world");
?>
And the headers are being sent:
$ curl -I ubuntu-server.lan/test.php
HTTP/1.1 200 OK
Date: Sun, 25 Nov 2018 09:37:52 GMT
Server: Apache/2.4.18 (Ubuntu)
Cache-Control: no-cache,must-revalidate
X-Clacks-Overhead: "GNU Terry Pratchett"
X-XSS-Protection: 1;mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Expect-CT: enforce,max-age=2592000
Content-Language: en
Referrer-Policy: origin-when-cross-origin
Content-Type: text/html; charset=UTF-8
Always keep in mind that when you change headers in .htaccess to also change them in headers.php.
Hope this helps!
➥ previous answer
I think this problem results from the httpd/apache2 headers_module not being loaded correctly (although you state otherwise in one of the above comments). You can check this by executing this command in the terminal:
apachectl -M | grep headers_module
If you get no output headers_module (shared) (or similar), then you have to activate the httpd/apache2 headers module. On a CentOS system you have to load the respective source file in your configuration (default /etc/httpd/conf/httpd.conf).
You have to add this line
LoadModule headers_module /usr/lib/apache2/modules/mod_headers.so
and then restart the http server wih sudo systemctl restart httpd.service
With EasyApache 4 the folder where httpd/apache2 modules are located might differ and be /usr/lib64/apache2/modules/.
I hope this helps!
It is not so much FastCGI as it is mod_proxy_fcgi, the method of asking Apache to "execute" FastCGI by passing it to some other listener.
When you use any mod_proxy* module, .htaccess isn't processed at all, because you're acting as a proxy and short-circuiting any disk-related configuration sections.
php-fpm will be looking at the request URL and reading data from disk, but Apache isn't. It is just confusing to people because they can be running on the same host and the files are often in a directory httpd could serve directly.
After much exploration it was found the issue was the PHP Handler -- the fastCGI (cgi) handler was not keeping the headers.
Changing to the suphp handler immediately resolved the issues.
I had same problem.
Please enable cache module in Linux Ubuntu.
sudo a2enmod cache
Then run:
sudo service apache2 start
Related
I know there is many solutions given regarding the same question but I tried all of them and none of them working at all.
I am tried following ways but none of them worked. My php version is 7.1 and Codeigniter framework I am using.
By setting header in index.php
header('Set-Cookie: HttpOnly; SameSite=None;Secure');
By setting in .htaccess
Header edit Set-Cookie ^(.*)$ "$1;HttpOnly;Secure;SameSite=none"
By setting in apache2 httpd.conf
Header edit Set-Cookie ^(.*)$ "$1;HttpOnly;Secure;SameSite=None"
I have reviewed Chrmoe git updates, it says
header('Set-Cookie: cross-site-cookie=bar; SameSite=None; Secure');
I didn't get the option cross-site-cookie=bar. What will be value for it.
I also tried the same one but it didn't work at all.
Hello I have solved this issue by following. Hope it will help to others
In httpd.conf (For bitnami server file will be /opt/bitnami/apache2/conf)
Header always edit Set-Cookie ^(.*)$ $1;Secure;SameSite=None
This might also help for someone still struggling, and using PHP >= 7.3.x and using CI 3.1.11
In the index.php found in the root, add the code below <?php
if(isset($_COOKIE["PHPSESSID"])){
header('Set-Cookie: PHPSESSID='.$_COOKIE["PHPSESSID"].'; SameSite=None');
}
It worked for me, after trying it all (in vain)
Paste the code below in your .htaccess file
<If "%{HTTP_USER_AGENT} !~ /(iPhone; CPU iPhone OS 1[0-4]|iPad; CPU OS 1[0-4]|iPod touch; CPU iPhone OS 1[0-4]|Macintosh; Intel Mac OS X.*Version\x2F1[0-3].*Safari|Macintosh;.*Mac OS X 10_14.* AppleWebKit.*Version\x2F1[0-3].*Safari)/i">
Header edit Set-Cookie ^(.*)$ $1;SameSite=None;Secure
</If>
This worked for me:
sudo nvim /etc/apache2/conf-available/security.conf
Header set Set-Cookie "mycookie=myvalue; Domain=mydomain.com; Path=/; Secure; HttpOnly; SameSite=Strict"
sudo systemctl restart apache2
My site works with ip but not with domain.
When I curl -v 'example.com' (with my domain in place of example) I get:
* Rebuilt URL to: example.com/
* Trying 123.123.123.123...
* Connected to example.com (123.123.123.123) port 80 (#0)
> GET / HTTP/1.1
> Host: example.com
> User-Agent: curl/7.47.0
> Accept: */*
>
* Empty reply from server
* Connection #0 to host example.com left intact
curl: (52) Empty reply from server
However, when I curl -v '123.123.123.123' (with my ip in place of 123.123.123.123) all works fine and I get:
* Rebuilt URL to: 123.123.123.123/
* Trying 123.123.123.123...
* Connected to 123.123.123.123 (123.123.123.123) port 80 (#0)
> GET / HTTP/1.1
> Host: 123.123.123.123
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx
< Date: Sun, 17 Sep 2017 06:45:56 GMT
< Content-Type: text/html; charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Vary: Accept-Encoding
<
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello World Test Page</title>
</head>
<body>
<h1>Hello World</h1>
<p>Test page.</p>
</body>
</html>
* Connection #0 to host 123.123.123.123 left intact
I get the same behavior in browsers. Hello world page shows with ip but not with domain.
What could I possibly be missing?
The hello world content returned when curling the ip is exactly the content of my index.php file as expected.
I set the DNS more than 48 hours ago so that shouldn't be the problem (and doesn't seem to be seeing curl returns the correct ip when queried on the domain).
I have tried adding a default .htaccess file with the content below but this didn't change the result.
# basic compression
<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js)$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>
# Protect files and directories
<FilesMatch "(\.(engine|inc|info|install|module|profile|po|sh|.*sql|theme|tpl(\.php)? |xtmpl)|code-style\.pl|Entries.*|Repository|Root|Tag|Template)$">
Order allow,deny
</FilesMatch>
# Don’t show directory listings
Options -Indexes
# Basic rewrite rules, stop unneeded PERL bot, block subversion directories
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*/)?\.svn/ - [F,L]
ErrorDocument 403 "Access Forbidden"
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* – [F,L]
</IfModule>
I have scraped stackoverflow for tonnes of similar questions but none that I've found seem to be the exact same issue.
Any ideas? Thanks in advance.
(PS: I'm really not sure about the tags that I've added coz I really don't know where the problem is).
You have to let curl follow any redirect by using -L
So
curl -v -L 'example.com'
First thing try to check that the domain is properly configured and resolving to the IP of your instance/server:
dig example.com +short
That should give you the IP of your server in case you are not using a CDN something like CloudFlare
If that is working check that you have the web server properly configured to receive requests from your domain, in case you were using Nginx you could have something like this:
server {
listen 80;
server_name *.example.com;
...
}
Notice the server_name *.example.com
I write an API with PHP ZF2 they use HTTP Authorization. I fetch all HTTP Headers with apache_request_headers() (also tested with ZF2's $this->getRequest()->getHeaders()).
It works on my locale installed version. But on my server the HTTP Authorization Header are not available. My Browser Debug tool show me that the Authorization header properly send.
Both server are running with the same software: Ubuntu 14.04 with Apache2 (Server version: Apache/2.4.7 (Ubuntu)).
Enabled apache2 modules (auth_basic is enabled):
Is there a PHP ini setting to allow Authorization header?
edit 2015-05-13:
$headers = apache_request_headers();
if (isset($headers['Authorization'])) {
echo 'you are auth';
} else {
echo 'there is no Authorization';
}
On my locale system this returns 'you are auth', on the server 'there is no Authorization'. Tested with Postman app in Chrome browser.
edit 2015-05-14:
I think it is an Apache2 topic.
How can i enable the Authorization header in Apache2?
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
in the .htaccess solves the problem.
But i do not know why this is not necessary on my locale system.
As bitkorn suggested, you can add the following to your .htaccess:
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
If that doesn't solve your problem, then you can try the following:
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
However, something that must be mentioned is that if you're using either solution, you must access your header with the HTTP_AUTHORIZATION header. If you try to use Authorization it will be null.
Server: Almalinux 8, Panel: WHM/CPANEL;
The reason is apache. Something removes the header. To prevent;
Open httpd.conf
<VirtualHost>
# ...
Include "/etc/apache2/conf.d/userdata/*.conf"
# ...
</VirtualHost>
place will be detected by apache.
And create a special conf to prevent removed automatically.
nano /etc/apache2/conf.d/userdata/{username}.conf
Add this line;
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
Restart Apache;
service httpd restart
I have Apache (2.2.22 on Debian) configured to handle PHP files via FastCGI:
<FilesMatch ".+.php$">
SetHandler application/x-httpd-php
</FilesMatch>
Action application/x-httpd-php /fcgi-bin/php5-fpm virtual Alias
/fcgi-bin/php5-fpm /fcgi-bin-php5-fpm FastCgiExternalServer
/fcgi-bin-php5-fpm -socket /var/run/php5-fpm.sock -idle-timeout 600
-pass-header Authorization
To show a custom File Not Found (HTTP 404) page is configured in Apache as follows:
<Directory "/home/http/domain/root">
..
ErrorDocument 404 /pagenotfound.htm
..
</Directory>
Requests for non-existing non-PHP files are answered with the custom 404 pagenotfound.htm file. No problem.
But requests for non-existing PHP files are answered with http-status-header "HTTP/1.1 404 Not Found" and contents "File not found.", so not my custom error page. Problem!
The Apache error log shows (in the latter case):
[Sat Nov 21 14:03:07 2015] [error] [client xx.xxx.xx.xx] FastCGI: server "/fcgi-bin-php5-fpm" stderr: Primary script unknown
How can I configure a custom 404 page for non-existing PHP files when using PHP-FPM?
set "ProxyErrorOverride on" in either your global server config or in individual virtual hosts, see http://httpd.apache.org/docs/current/mod/mod_proxy.html#proxyerroroverride
When 'File not found' is shown instead of custom error page for non-existing .php files (and all other non-existing files get the correct custom error page)...
Centos 8, PHP 7.2.11
File: /etc/httpd/conf.d/php.conf
Add 'ProxyErrorOverride On' after the SetHandler
<FilesMatch \.(php|phar)$>
SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost"
ProxyErrorOverride On
</FilesMatch>
Not sure if required, but I then did:
systemctl restart httpd
Option: ProxyErrorOverride
ProxyErrorOverride can be used if you have access to the server's configuration. But it doesn't work within the .htaccess context and it will prevent PHP from outputting dynamic response bodies for all configured status codes (default: 400 to 599).
Option: <If> directive (Apache 2.4+)
Let Apache check if the file exists, before invoking PHP:
<Files "*.php">
<If "-e %{REQUEST_FILENAME}">
# Assuming PHP-FPM over Unix socket via mod_proxy_fcgi.
SetHandler proxy:unix:/path/to/php-fpm.sock|fcgi://
</If>
<Else>
# Ensure that *.php files are never handled by the default handler.
Redirect 404
</Else>
</Files>
Docs: <If>, Expression parser, Redirect
Option: mod_rewrite
The ErrorDocument can be triggered using mod_rewrite:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule "\.php$" - [R=404]
Note: This only works if REQUEST_FILENAME has already been determined (so not in the server config or virtual host, but in a directory or .htaccess context). Otherwise it is equal to REQUEST_URI and that probably wouldn't be an existing local file.
I have hosted my wordpress blog in heroku , everything works fine but intermittently I get 404 errors while accessing wordpress pages e.g. following return 404 (The requested URL /about/ was not found on this server.) most of the time
http://pacific-wildwood-3863.herokuapp.com/about/
When I see the heroku log it has following
Jul 16 21:40:06 pacific-wildwood-3863 app/web.1: [Wed Jul 17 04:40:05
2013] [error] [client 10.62.147.42] File does not exist:
/app/www/about, referer: http://pacific-wildwood-3863.herokuapp.com/
Jul 16 21:40:06 pacific-wildwood-3863 app/web.1: 10.62.147.42 - -
[17/Jul/2013:04:40:05 +0000] "GET /about/ HTTP/1.1" 404 204 Jul 16
21:40:08 pacific-wildwood-3863 heroku/router: at=info method=GET
path=/about/ host=pacific-wildwood-3863.herokuapp.com
fwd="182.72.242.3" dyno=web.1 connect=5ms service=3ms status=404
bytes=204
Not sure why it looks for about directory instead of being processed by wordpress. Is there any wordpress path configuration missing or I need to increase web dynos, currently I have only 1 ? Note that it works sometimes ?
I also had this problem, quite a pain. Changing the permalinks works for a while, but your links are broken again once Heroku restarts dynos. I did the following to solve it:
1) Change your .htaccess file into:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
2) Make sure you save the .htaccess file encoded as ANSI (Look for "Western" + "Windows" when saving from TextEdit on Mac).
3) Commit changes and push to Heroku.
4) You can check whether it works by running heroku restart.
Hope that helps!
Going through this forum it surprisingly worked when I checked the permalink option to another one.
It also worked when I changed back, not sure what was wrong.
Please check that you're not blocking the htaccess file in a git ignore. if you are, comment out the ignore then commit and push the htaccess file. That should fix your issue.
I use bedrock and had this same issue as it ignores the htaccess as default.
Hope that fixes it for you!