Forum Moderators: phranque

Message Too Old, No Replies

redirect using htaccess, but not 301

(visitor should be unaware of redirect)

         

siMKin

7:19 am on May 5, 2006 (gmt 0)

10+ Year Member



I want to use a htaccess file to redirect certain requests to a specific file, but not in the way a 301 does it. i want to do it 'silently', so to speak. The user - and especially search engines - are not to be aware of this.

The reason is that i want to log requests to certain .doc and .pdf files. I've saved their contents in a database and what i want to do now is this:

if a visitor request, for example, file_this_and_that.doc, htaccess should (silently) redirect to download.php - this will look for that name in the database, output the contents and log te request.
But: the visitor (or search engine) should not really be redirected to download.php. In the locationbar it should still say file_this_and_that.doc

(btw, everything else (download.php) already works. It's just that i need an alternative for the 301

jdMorgan

2:25 pm on May 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Part of the problem is that this application does not call for an external redirect, but rather for an internal rewrite. So, getting the terminology right will make it easier to search for and find answers.

An external redirect sends an HTTP redirect response to the client (e.g. browser or search robot), telling it to re-request the desired content using a new URL supplied within that redirect response. Thus, the client becomes aware of the new URL, and in the case of a browser, the new URL is displayed to the user. The receipt of this redirect response by the client ends the current HTTP transaction. If the client opts to re-request the desired content using the new URL, this request defines a new HTTP transaction.

By contrast, an internal rewrite changes only the filename (or the path to a script) that is associated with the originally-requested URL. This is done entirely within the context of the original HTTP request. As such, it is 'silent,' since the client --and therefore, the user-- are unaware that the request has been rewritten inside the server. This can only be used within the server; It is impossible to do an internal rewrite to another domain, since that is a contradiction in terms (**See note).

Apache mod_rewrite supports both functions, as well as several others. The only difference in coding an external redirect versus an internal rewrite is in the syntax:

External Redirect:

 RewriteRule ^file_this_and_that\.doc$ http://www.example.com/download.php [R=301,L]

Internal Rewrite:

RewriteRule ^file_this_and_that\.doc$ /download.php [L]

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

** Note: See RewriteRule's [P] flag for a possible solution using a proxy request.

Jim

siMKin

6:39 pm on May 6, 2006 (gmt 0)

10+ Year Member



thanks, this cleared up a lot for me!
i cannot access the server right now, but i'm gonna try this new knowledge first thing on monday :-)