虚拟主机中的Apache RewriteRule

问题描述 投票:0回答:1

我正在使用专用的网络服务器。我网站的Apache虚拟主机是:

<VirtualHost *:80>
    ServerName www.domain.com
    DocumentRoot /var/www/domain.com/
    ErrorLog /var/log/httpd/domain.com.error.log
    CustomLog /var/log/httpd/domain.com.access.log combined
</VirtualHost>

在我网站的根目录中,我有一个.htaccess文件,其中包括:

<IfModule mod_headers.c>
    RewriteEngine On

    # Serve gzip compressed CSS and JS files if they exist and the client accepts gzip.
    RewriteCond "%{HTTP:Accept-encoding}" "gzip"
    RewriteCond "%{REQUEST_FILENAME}\.gz" -s
    RewriteRule "^(.*)\.(css|js)"         "$1\.$2\.gz" [QSA]

    # Serve correct content types, and prevent mod_deflate double gzip.
    RewriteRule "\.css\.gz$" "-" [T=text/css,E=no-gzip:1]
    RewriteRule "\.js\.gz$"  "-" [T=text/javascript,E=no-gzip:1]

    <FilesMatch "(\.js\.gz|\.css\.gz)$">
      # Serve correct encoding type.
      Header append Content-Encoding gzip

      # Force proxies to cache gzipped and non-gzipped css/js files separately.
      Header append Vary Accept-Encoding
    </FilesMatch>
</IfModule>

它在.htaccess文件中可以很好地工作,但是如果我将此代码移至vhost.conf(虚拟主机文件),它将停止工作。

如何在vhost.conf中进行重写以避免需要.htaccess文件?我认为这与文件位置或文档根目录有关吗?

apache vhosts apache2.4
1个回答
0
投票

默认重写配置不会被虚拟主机继承。因此,每个虚拟主机配置都需要:RewriteEngine on and RewriteOptions Inherit.

只需在虚拟主机配置文件中尝试此代码:

<VirtualHost *:80>
    ServerName www.domain.com
    <IfModule mod_headers.c>
        RewriteEngine on
        RewriteOptions Inherit
        # Serve gzip compressed CSS and JS files if they exist and the client accepts gzip.
        RewriteCond "%{HTTP:Accept-encoding}" "gzip"
        RewriteCond "%{REQUEST_FILENAME}\.gz" -s
        RewriteRule "^(.*)\.(css|js)"         "$1\.$2\.gz" [QSA]

        # Serve correct content types, and prevent mod_deflate double gzip.
        RewriteRule "\.css\.gz$" "-" [T=text/css,E=no-gzip:1]
        RewriteRule "\.js\.gz$"  "-" [T=text/javascript,E=no-gzip:1]
    </IfModule>
    <FilesMatch "(\.js\.gz|\.css\.gz)$">
        # Serve correct encoding type.
        Header append Content-Encoding gzip

        # Force proxies to cache gzipped and non-gzipped css/js files separately.
        Header append Vary Accept-Encoding
    </FilesMatch>
</VirtualHost>
© www.soinside.com 2019 - 2024. All rights reserved.