How to get .htaccess files working with Apache VirtualHost

痞子三分冷 提交于 2019-12-22 03:43:32

问题


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?


回答1:


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




回答2:


You miss the RewriteBase / directive

Add it after the RewriteEngine directive :

RewriteEngine On
RewriteBase /


来源:https://stackoverflow.com/questions/14419757/how-to-get-htaccess-files-working-with-apache-virtualhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!