Hiding a php file -- help!

New to mod_rewrite? This is a good place to start.

Postby Andy Serpa » Mon Nov 19, 2001 7:01 pm

Hello,

I want to achieve the following effect:

If the user goes to "script.php" he gets a 404 not found error.

If the user goes to "page.htm" he gets the output of script.php.

In other words, I want to hide the fact that script.php exists at all, but still be able to direct its output to page.htm.

I am working on a per-directory basis with .htaccess.

This rule works fine by itself:

RewriteRule ^script.php$ nonexistent.htm

nonexistent.htm doesn't exist, and the user gets 404 if he types "script.php" in the browser.

This rule also works fine by itself:

RewriteRule ^page.htm$ script.php [T=application/x-httpd-php]

If the user goes to page.htm (which doesn't exist), he gets the output of script.php, but the location bar still says "page.htm".

However, if I combine the rules in the .htaccess file to cover both possibilities it doesn't work:

RewriteRule ^script.php$ nonexistent.htm [L]
RewriteRule ^page.htm$ script.php [T=application/x-httpd-php,L]

Notice I added the "L" flags, but now I get a 404 in both cases --> page.htm becomes script.php becomes nonexistent.htm and 404. (I verified this by setting nonexistent.htm to a real page, and it always went there if you went to either page.htm or script.php).

Originally I wanted script.php to become page.htm in the browser but keep the same output, but I ended up with an infinite loop, even with the "L" flags, which I don't understand...

Can anyone explain if what I want is possible or what I'm doing wrong? Why is the rule pointing to the nonexistent page always invoked? According to the docs, they are processed in order, and I'm not using re-directs. (I tried the rules in opposite order, same result.)


Andy Serpa
chessmad@chessopolis.com
http://www.chessopolis.com
Andy Serpa
 
Posts: 3
Joined: Sun Nov 18, 2001 4:00 pm

Postby Brett » Sun Nov 25, 2001 7:27 pm

Hi Andy,

As you've noticed, the [L] tag doesn't work the way you want it to. Once rewritten to script.php, your rewrite rules are re-applied starting at the top.

One way to solve the conundrum is to edit your PHP script, instead of asking mod_rewrite to do all the work. You could add something like this to the top of your PHP document:
Code: Select all
<?php
if ( $REQUEST_URI == '/script.php' ) {
   header("HTTP/1.0 404 Not Found");
   echo("Error 404: File does not exist");
   exit;
}
?>


Another way might be to simply rename your script as page.htm, and include the following line in your .htaccess file:
Code: Select all
AddType application/x-httpd-php .htm


<font size=-1>[ This Message was edited by: Brett on 2001-11-25 22:28 ]</font>
Brett
 
Posts: 82
Joined: Tue Jul 10, 2001 4:00 pm
Location: yohost.com

Postby Andy Serpa » Sun Nov 25, 2001 7:45 pm

Yeah,

I did figure out a work-around by adding a RewriteCond to one of the rules, but how come the [L] flag doesn't work? According the mod_rewrite docs, it should *not* start again at the top, but continue to keep processing to the next rule and if [L] flag is used simply stop altogether.

My guess is that it only acts properly when used on a server basis instead of a directory basis in .htaccess files because of the way Apache works...
Andy Serpa
 
Posts: 3
Joined: Sun Nov 18, 2001 4:00 pm

Postby Brett » Sun Nov 25, 2001 8:12 pm

The [L] tag means that once the rule is applied, any directives that follow (within the same .htaccess file) will be ignored. But, once the rewriting has taken place, the server treats it like a new request, so the process starts again.

One trick for avoiding this kind of "trap" is to rewrite to another directory whose .htaccess contains RewriteEngine off.

To be honest, I've only used the .htaccess version. It may behave differently in a httpd.conf file.

Here are some of the gory details.
Brett
 
Posts: 82
Joined: Tue Jul 10, 2001 4:00 pm
Location: yohost.com

Postby Andy Serpa » Sun Nov 25, 2001 8:24 pm

It is those exact gory details from the Apache mod_rewrite manual that I am making my guess -- it sounds as though the .htaccess file comes rather late in the process, but who knows? It's all voodoo!
Andy Serpa
 
Posts: 3
Joined: Sun Nov 18, 2001 4:00 pm


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 90 guests

cron