Where do I put RewriteRules in Apache2 without using .htaccess files?

devin picture devin · Sep 24, 2010 · Viewed 9.3k times · Source

I want to configure a mod_rewrite rule without using .htaccess files. When I put rules in .htaccess files they work fine, but I would prefer to leave all of the configuration in my /etc/apache2/sites-available/[site name] config file.

When I place the same RewriteRules within the VirtualHost or Directory directives, nothing works. What am I doing wrong? Here is a sample from my VirtualHost config file:

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

I'm thinking I might be overlooking some directive within the apache2.conf file, but I'm not sure. Help. :)

Answer

Gumbo picture Gumbo · Sep 24, 2010

You’re using a RewriteRule pattern that is meant for an .htaccess file. The reason:

When using the rewrite engine in .htaccess files the per-directory prefix (which always is the same for a specific directory) is automatically removed for the pattern matching and automatically added after the substitution has been done.

So try this rule with the full URL path:

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