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

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)

  • One thing to make this a bit easier to debug would be to attach some of your logs. Also, when you say its not working, how are you trying to access the URL, etc.? Finally, Apache versions would be useful as well. – Andrew M. May 23 '11 at 20:55
  • Added the info! – user60129 May 24 '11 at 0:09
  • Does apache complain about anything when restarting/reloading it after the change? – Eduardo Ivanec May 24 '11 at 0:45
  • Nope, it restarts without any warning. All OK. – user60129 May 24 '11 at 0:48
  • Does it work if you put it outside the Directory scope? – Eduardo Ivanec May 24 '11 at 1:00

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.

  • Thanks for your reply. The very weird thing was that the thing actually started working after a while, without any other change. It is as if there is some cache, but on Apache's side (I had used different browsers to test and it did not work in any, and then all of a sudden things worked). I had restarted Apache way long before, so that is not the case either. Is there some hidden cache that makes Apache think it should not do a redirect despite the config file?? Or I am overseeing something? – user60129 Jun 3 '11 at 3:01
  • I've seen exactly this because of client-side browser caching. I know this is an old question, but for new visitors, clear your browser cache to test redirects. – Bob Brown Dec 31 '15 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy