2014-09-12 101 views
1

我試圖在我的Apache服務器上使用.htaccess文件。.htaccess被忽略 - Ubuntu 14.04

這裏是我的.htaccess的樣子

# 1 YEAR 
<FilesMatch "\.(ico|svg|woff|eot|ttf)$"> 
Header set Cache-Control "max-age=31536000, public" 
</FilesMatch> 

# 1 WEEK 
<FilesMatch "\.(jpg|png|gif|css|js)$"> 
Header set Cache-Control "max-age=604800, public" 
</FilesMatch> 

# Add correct content-type for fonts 
AddType application/vnd.ms-fontobject .eot 
AddType font/ttf .ttf 
AddType font/otf .otf 
AddType font/x-woff .woff 
AddType image/svg+xml .svg 

# Compress compressible fonts 
AddOutputFilterByType DEFLATE font/ttf font/otf image/svg+xml 

我能使用a2enmod rewrite

我也跟着叫我來編輯/etc/apache2/sites-available/default文件的教程mod_rewrite的,但有沒有名字default文件在該文件夾中。有一個000-default.conf而不是相同的路徑。但該文件沒有部分

<Directory /var/www/> 
    Options Indexes FollowSymLinks 
    AllowOverride None 
    Require all granted 
</Directory> 

我應該編輯。

這是000-default.conf

<VirtualHost *:80> 

    # The ServerName directive sets the request scheme, hostname and port that 
    # the server uses to identify itself. This is used when creating 
    # redirection URLs. In the context of virtual hosts, the ServerName 
    # specifies what hostname must appear in the request's Host: header to 
    # match this virtual host. For the default virtual host (this file) this 
    # value is not decisive as it is used as a last resort host regardless. 
    # However, you must set it for any further virtual host explicitly. 
    #ServerName www.example.com 

    ServerAdmin [email protected] 

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, 
    # error, crit, alert, emerg. 
    # It is also possible to configure the loglevel for particular 
    # modules, e.g. 
    #LogLevel info ssl:warn 

    ErrorLog ${APACHE_LOG_DIR}/error.log 
    CustomLog ${APACHE_LOG_DIR}/access.log combined 

    # For most configuration files from conf-available/, which are 
    # enabled or disabled at a global level, it is possible to 
    # include a line for only one particular virtual host. For example the 
    # following line enables the CGI configuration for this host only 
    # after it has been globally disabled with "a2disconf". 
    #Include conf-available/serve-cgi-bin.conf 

</VirtualHost> 

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet 

內容然而/etc/apache2/apache2.conf有完全相同的一部分。所以我把AllowOverride None換成了AllowOverride All。之後我重新啓動了服務器。

但是.htaccess文件仍未加載。如果我添加亂碼到.htaccess文件,一切仍然正常工作,這意味着它沒有得到加載。

我在這裏錯過了什麼?

回答

2

isssue是我將.htaccess文件放在/var/www/而我的網站DocumentRoot指向一個不同的目錄。該.htaccess文件移動到該文件夾​​解決了問題。

3

首先,文件的名稱並不重要。 000-default.conf,雖然不是很常見,但對我來說似乎很好。

修改網站配置

有2件主要的事情,你有服務.htaccess文件時要考慮:

AccesFileName .htaccess 
AllowOverride All 

apache2 doc說,你必須在目錄申報AllowOverride部分。這就是爲什麼它在主配置文件中設置時不起作用的原因。

我建議你只貼上這4條線在/etc/apache2/000-default.conf虛擬主機段內,它應該工作:

<Directory /var/www/> 
    Options Indexes FollowSymLinks 
    AllowOverride All 
</Directory> 

(假設你的根目錄是/ var/www)

您不必設置AccessFileName默認是的.htaccess

如果它不工作,你有超過1個文件中/etc/apache2/sites-available

的Apache可能會使用另一個配置吳丹000-default.conf。只需檢查其中哪些符號鏈接到了/etc/apache2/sites-enabled。如果仍然超過1個,您可能需要禁用其他每個啓用的網站才能確定。

如果仍然不工作,只是檢查文件的所有權&權限。