Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache mod_rewrite works in .htaccess but not httpd.conf

I'm experimenting with mod_rewrite for the first time (I'm a web newbie, but trying to learn). I'm trying to get bob.html to redirect to alice.html (read: URL stays the same, page content is alice.html). Both files are in /var/www/. I'm running Ubuntu 10.10 w/Apache 2.2.16.

Here's what works in the .htaccess file placed in the /var/www/ directory:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^bob.html$ alice.html
</IfModule>

This behaves as expected, but it does not work when moved to the httpd.conf file (just learning best practices w/performance and such). I read somewhere that when using mod_rewrite in httpd.conf file, leading slashes are required, so my httpd.conf file looks like:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/bob.html$ /alice.html
</IfModule>

I also read that /etc/apache2/sites-enabled/000-default needs to have AllowOverride set to All.

<Directory /var/www> 
...
AllowOverride All
...
</Directory>

I think that's just for use with .htaccess but I wasn't sure so I just left it in.

With all of these configuration settings, the redirection does not work. I've tested to make sure the file itself is being read (someone suggested httpd.conf isn't used anymore and apache2.conf is used instead) by inserting erroneous code. I've taken a look at a couple other questions/answers but I still cant figure it out.

Edit: It should be noted that I'm using /etc/init.d/apache2 restart after each change to httpd.conf to restart Apache and (hopefully) reload the configuration.

like image 220
Chris Avatar asked Feb 25 '11 18:02

Chris


1 Answers

Try wrapping the rewriterules in your httpd.conf file in <Location /></Location> tags. The seems to alter the behavior making it more similar to the .htaccess file.

like image 189
Dave Kok Avatar answered Nov 07 '22 14:11

Dave Kok