Apache permanent redirect not working

301-redirectapache-2.2

Trying to redirect an old URL in Apache. But I can not figure out what is wrong with this:

<Directory "/home/site/www/">
RewriteEngine On
Options +FollowSymLinks
Redirect permanent /test http://www.google.com
</Directory>

Redirecting is not working at all. Am I missing something??

I type in the browser http://site/test and it gives me a 404-not-found error. From the Apache Log:

[Mon May 23 18:07:09 2011] [error] [client XXXXXX] File does not exist: /home/site/www/test

Apache/2.2.16 (Ubuntu)

Best Answer

I haven't encountered this before, but the fact that it does work when you put the redirect outside the Directory directive scope seems to point out that Apache is not matching that Directory directive when it receives /test as a request. The docs say that Directory should match when the request points to the specified directory or some subdirectory therein - but Apache is probably first checking whether test is a valid subdirectory, and not finding it it doesn't look inside that block.

I'm not seeing why leaving the redirect outside the block doesn't do what you want - isn't /home/site/www the document root? If you do need to limit this redirection to some part of your site use Location instead of Directory - it handles URLs independently of the physical path in your server, which is probably what you need considering that test doesn't really exist in your server.

If in need of a more complex redirection (for example if you want to redirect all URLs starting with /test, and not only the requests pointing exactly to /test) consider using RedirectMatch instead:

RedirectMatch ^/test.* http://www.google.com

Redirect would redirect /test/example to http://www.google.com/example instead of to plain http://www.google.com, which may or may not be what you intended. This RedirectMatch discards whatever comes after /test.