How to get .htaccess files working with Apache Vir

2019-04-20 15:09发布

My problem is that my .htaccess file on my local server is not being read. The settings in the VirtualHost file seem to always take precedence.

I have tried the following:

  • Enabled mod_rewrite
  • Changed the AllowOverride to All but this causes a HTTP Error 500 Internal server error. I have tried it with various options but it always causes a 500 error.

I am using a VirtualHost file on Ubuntu which looks like the following:

<VirtualHost *:80>
ServerAdmin webmaster@localhost

DocumentRoot /web/website
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
<Directory /web/website>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>
</VirtualHost>

In my .htaccess file under /web/website I have the following rules (which are not being read):

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{HTTP_USER_AGENT} ^facebookexternalhit
RewriteRule ^(.*)$ ogtags.php?$1 [L,QSA]
ErrorDocument 404 /404
ErrorDocument 401 /401

One thing I tried which did work was appending these rules directly into the VirtualHost file, but I would like my .htaccess file to work! Is that such a big ask? :(

Edit: So I looked in my apache error.log and it says Invalid command 'Action', perhaps misspelled or defined by a module not included in the server configuration referring to my .htaccess file. There doesn't seem to be a module called Action which I can enable. Any ideas?

Edit 2: I noticed that my httpd.conf file is blank. Should this matter since I am using VirtualHost files?

2条回答
Root(大扎)
2楼-- · 2019-04-20 15:53

You miss the RewriteBase / directive

Add it after the RewriteEngine directive :

RewriteEngine On
RewriteBase /
查看更多
来,给爷笑一个
3楼-- · 2019-04-20 16:01

After looking at my apache error.log I realised I just had to enable the Apache actions module:

sudo a2enmod actions

And no more 500 Internal Server Errors!

Hope this helps somebody down the line :)

查看更多
登录 后发表回答