views:

18

answers:

1

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. :)

A: 

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
Gumbo
Thank you very much for the fast response. It wouldn't work when I put it within the Directory directive, but I moved it up to VirtualHost and it is now working fine. Any idea why?
wittmaniac
@user358036: I guess you need to put it into the `<Directory>` section that describes your document root.
Gumbo