3

My .htaccess file is not being read, I have put a .htaccess file in /var/www/html which contains

Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

And then my http.conf file looks like this

ServerRoot "/etc/httpd"
<Directory />
AllowOverride none
Require all denied
</Directory>

DocumentRoot "/var/www/html"

<Directory "/var/www">
AllowOverride None
# Allow open access:
Require all granted
</Directory>

<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>

And the file I want to access in is in my /var/www/html folder but when I try to access it form another sever I keep getting this error

Image from origin 'http://serverB' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.com' is therefore not allowed access.

Why isn't my .htacces being read and how can I make it be read?

Thanks

DaveR
  • 9,540
  • 3
  • 39
  • 58
iqueqiorio
  • 1,149
  • 2
  • 35
  • 78

3 Answers3

2

You need to allow the AllowOverride All in the /html file as well, Im not sure why but I had the same problem also make sure to restart the apache after you save the .htaccess file otherwise the changes will not apply.

Also make sure that you do not have any other .htaccess files anywhere else as using add could add the headers extra time cause error.

ghjghgkj
  • 373
  • 1
  • 3
  • 9
1

Try adding this line to the .htaccess file.

I was facing the same issue whenever I use reverse proxy since it was my dev machine I just add these lines into the .htaccess file and it solved this issue.

<FilesMatch "\.(php)$">
  <IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
  </IfModule>
</FilesMatch>
Umesh .A Bhat
  • 587
  • 3
  • 15
  • 27
Nix Nikhil
  • 36
  • 4
0

Is mod_headers enabled on the server? check with this command:

apache2ctl -M

If not, enable it using this command:

sudo a2enmod headers

After that restart the apache server.

If it still does not work checkout the following answer: https://stackoverflow.com/a/13871027/2507790

Community
  • 1
  • 1
Vikas
  • 993
  • 1
  • 10
  • 16
  • I got `-bash: apache2ctl: command not found` when running the first command and `sudo: a2enmod: command not found` when running the second command – iqueqiorio Dec 07 '15 at 08:08
  • use this for centos: httpd -M – Vikas Dec 07 '15 at 08:19
  • when I do that I got a long list of moduels – iqueqiorio Dec 07 '15 at 08:31
  • Checkout this answer for a better understanding of apache modules on centos: http://serverfault.com/a/56435/325169 . And just to make sure that htaccess is being read by the server, try something else, maybe a redirect from the htaccess file – Vikas Dec 07 '15 at 08:37