SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ErrorDocument, preserving environment variables

    Apache 2.2
    Code XML:
    <VirtualHost foo.localhost:80>
      NameVirtualHost foo.localhost
      DocumentRoot /Users/samsm/Sites
     
      RewriteEngine On
      RewriteRule ^/test_forward$ /test.php [QSA,L]
     
      ErrorDocument 404 /test_forward
     
    </VirtualHost>

    In this example, test.php gets run on a 404, and kindly provided with a REDIRECT_URL server variable, accessible at $_SERVER['REDIRECT_URL']. THis is what you'd expect from http://httpd.apache.org/docs/2.2/cus....html#behavior

    Great except I want to take that REDIRECT_URL and use it in the rewrite.
    Like:
    Code XML:
    RewriteEngine On
    RewriteRule ^/test_forward$ /%{REDIRECT_URL}/test.php [QSA,L]
    But that doesn't work.

    Any ideas? Can you use arbitrary environment variables in rewrites? Or am I asking for something that just doesn't work?

    Is there another way to intercept 404s and rewrite to another resource?

    Thanks!
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  2. #2
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    While a tad messy, &#37;{THE_REQUEST} seems to have what I need.

    RewriteCond %{THE_REQUEST} (.*)
    RewriteRule ^/test_forward$ /test_forward/original/%1 [C]
    RewriteRule ^/test_forward(.*)$ /test.php$1 [P,QSA,L]
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  3. #3
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    ss,

    Oh, my! That makes no sense at all!

    First, I've never seen a domain name listed in the VirtualHost opening tag - always the IP Address (that may be because I've lead such a sheltered life ).

    Second, you can write your own 404 processing using mod_rewrite's -d and -f metacharacters (existing directory and file checks) and the {REQUEST_URI} (or {THE_REQUEST}) to pass info - I'd put it in a query string, though, and not add it as a directory/file after a filename (MultiViews doesn't like that).

    If you need the code, have a look at the tutorial Article linked in my signature - it's in the code section (last half).

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  4. #4
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got the domain name in the Virtualhost opening tag thing from a common Rails setup. My guess is it resolves that domain once at startup, which probably what most people would do manually. But, now that you mention it, I guess that would break in some circumstances.

    Quote Originally Posted by dklynn View Post
    Second, you can write your own 404 processing using mod_rewrite's -d and -f metacharacters (existing directory and file checks) and the {REQUEST_URI} (or {THE_REQUEST}) to pass info
    This would be a good idea, but in some cases I have to account for application logic determining that there is a 404.

    That %{request} data may well end up in a query string, that's just a detail I have worked out yet. Hopefully I'll have a prototype this weekend and I can demonstrate the sense of this.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  5. #5
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    ss,

    Go for it!

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •