如果不使用.htaccess文件,我在哪里将RewriteRules放在Apache2中?

时间:2010-09-24 17:55:31

标签: mod-rewrite apache2 virtualhost

我想在不使用.htaccess文件的情况下配置mod_rewrite规则。当我在.htaccess文件中放置规则时,它们工作正常,但我更愿意将所有配置保留在/ etc / apache2 / sites-available / [site name]配置文件中。

当我在VirtualHost或Directory指令中放置相同的RewriteRules时,没有任何作用。我究竟做错了什么?以下是我的VirtualHost配置文件中的示例:

<Directory />
 Options FollowSymLinks
 # AllowOverride is on for the .htaccess files to work
 AllowOverride All
 RewriteEngine On
 RewriteRule ^oldsite\.php$ newsite.php
</Directory>

我想我可能会忽略apache2.conf文件中的一些指令,但我不确定。救命。 :)

1 个答案:

答案 0 :(得分:5)

您正在使用RewriteRule pattern that is meant for an .htaccess file。原因是:

  

在.htaccess文件中使用重写引擎时,每个目录前缀(对于特定目录始终相同)会自动删除以进行模式匹配并自动添加替换完成后。

请尝试使用完整的网址路径:

RewriteRule ^/oldsite\.php$ /newsite.php