Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

jdavidb (1361)

jdavidb
  (email not shown publicly)
http://voiceofjohn.blogspot.com/

J. David Blackstone has a Bachelor of Science in Computer Science and Engineering and nine years of experience at a wireless telecommunications company, where he learned Perl and never looked back. J. David has an advantage in that he works really hard, he has a passion for writing good software, and he knows many of the world's best Perl programmers.

Journal of jdavidb (1361)

Monday March 21, 2005
10:18 PM

Apache .htaccess Redirect directive

[ #23779 ]

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 http://en.wikipedia.org/wiki/Main_Page. 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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • 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).
    --
    • Randal L. Schwartz
    • Stonehenge
    • 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.

      --
      J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
  • 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 happ
    • 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.

      --
      J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
      • 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.
        • Thank you. I will look into those to see if they better provide what I want.

          --
          J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers