Apache .htaccess Redirect directive

jdavidb on 2005-03-22T03:18:38

This is one of those entries I make after finally learning how to do something I've wanted to know how to do for awhile. I make these entries for two reasons: 1) it's theoretically possible I might forget what I learned, and 2) someone else might have been wondering the exact same thing, and if I had difficulty finding a place where the information was spelled out (no matter how available and easy to find the information might actually be), I think it's possible someone else might have had the same difficulty.

If you go to http://en.wikipedia.org (for example), you'll be redirected to . I know how to use HTML to make one page redirect to another, but it's flaky and seems to require a time-delay. (Changing the time-delay to 0 seems to work with some browsers and give seizures to others. Probably something I'm doing wrong.) And if you dig into the internals of an HTTP session with port 80 on en.wikipedia.org, you'll see that what it's doing isn't anything like sending an HTML document with redirect information.

So here's how to do exactly what Wikipedia (and probably trillions of other sites) does: create (if it doesn't exist) the .htaccess file in the directory you want to redirect. Then add this line:

Redirect 301 /index.html http://destination.site.example.com/path/to/page/

If you leave out the 301, the browser will be told the page has moved temporarily. If you include the 301, the browser will be told the page has moved permanently.


Not quite

merlyn on 2005-03-22T03:38:09

More likely, it's the equivalent of:
RedirectMatch ^/(index.html)?$ http://en.wikipedia.org/wiki/Main_Page
because otherwise, it'd be an infinite loop (Redirect matches a prefix).

Re:Not quite

jdavidb on 2005-03-22T03:59:49

Ah, I see. The trailing slash in my example does indeed cause that problem (which I also discovered when I was trying to get this to work, and I simply redirected / to /dir/). In the real-life versions I set up tonight, this wasn't an issue.

Pointless redirects

vsergu on 2005-03-22T23:21:52

Wikipedia may have its reasons, but there are far too many sites that redirect their front pages to some other URL. Why can't these people just set up their servers to give me the right content when I ask for "/" in the first place?

Even worse, visitors will bookmark the URL they get redirected to and start linking to that, and then six months later when the site changes to some other software the URL will stop working and the web will be filled with broken links to the site. (Not that that's likely to happen with Wikipedia.)

Re:Pointless redirects

jdavidb on 2005-03-23T14:54:09

Wikipedia may have its reasons, but there are far too many sites that redirect their front pages to some other URL. Why can't these people just set up their servers to give me the right content when I ask for "/" in the first place?

In my case it's because I'm using a cheapo hosting service and don't have that kind of access to the server.

Re:Pointless redirects

vsergu on 2005-03-23T15:07:22

I don't know the details of what you're doing, but if you have access to use Redirect in an .htaccess file there may be ways to get what you want without a redirect. For example, you can use DirectoryIndex inside .htaccess. You may even have mod_rewrite available.

Re:Pointless redirects

jdavidb on 2005-03-23T17:41:23

Thank you. I will look into those to see if they better provide what I want.