Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 481
devin Avatar asked Sep 24 '10 17:09

devin


1 Answers

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
like image 104
Gumbo Avatar answered Oct 07 '22 11:10

Gumbo