Apache 重写不起作用

Apache Rewrite does not work

我不想强制使用非 www,但它不起作用,这是我的 .conf 来自 sites-available。

<VirtualHost *:80>    
DocumentRoot /var/www/example
ServerName example.com
ServerAlias www.example.com
<Directory "/var/www/example">
Options -Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/ [L,R=301]
</Directory>
ServerAdmin admin@example.com

如果我的 vhost conf 和 .htacess 之间存在冲突,我也会向您展示我的 htaccess 代码:

<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

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

    # Uncomment the following line if server allows
    # This will compress js/css/html/php and increase site performance
    #<filesMatch "\.(js|css|html|php)$">
    #   SetOutputFilter DEFLATE
    #</filesMatch>

</IfModule>

你为什么同时使用两者?您应该将所有 .htaccess 代码放在配置中或全部放在 .htaccess 中。建议对所有配置使用配置并将 AllowOverride 设置为 None 以获得更好的性能。 .htaccess 主要用于共享主机,当您无权访问配置时。