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

10
  • ls -l will show permissions on files, maybe there will be a clue. Commented Dec 7, 2015 at 4:20
  • @ESYSCODER this listed the permissions of files in my root directory Im not sure how that helps?
    – iqueqiorio
    Commented Dec 7, 2015 at 4:27
  • I was thinking you will add results of it to the answer. Are permission on .htaccess allowing apache to read it? User/group match apache process user/group? Commented Dec 7, 2015 at 4:31
  • @ESYSCODER my .htaccess file is at /var/www/html not in /root and the owner and group of the .htaccess file is unknown do you I need to set that to apache?
    – iqueqiorio
    Commented Dec 7, 2015 at 4:38
  • Apache has to be able to read it, in /var/www/html do chmod 0644 .htaccess to allow everyone to read it or change user to apache user. Commented Dec 7, 2015 at 4:41

3 Answers 3

2
+100

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.

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>
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

4
  • I got -bash: apache2ctl: command not found when running the first command and sudo: a2enmod: command not found when running the second command
    – iqueqiorio
    Commented Dec 7, 2015 at 8:08
  • use this for centos: httpd -M
    – Vikas
    Commented Dec 7, 2015 at 8:19
  • when I do that I got a long list of moduels
    – iqueqiorio
    Commented Dec 7, 2015 at 8:31
  • Checkout this answer for a better understanding of apache modules on centos: 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
    Commented Dec 7, 2015 at 8:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.