PDA

View Full Version : Apache's mod_rewrite module & redirecting pages




whocares
Jan 14, 2005, 08:00 PM
Here's my problem: I would like to redirect all requests to a PHP script within a given folder, towards a predetermined page.

Let me explain. In the folder are many PHP scripts that are normally included into the main page; quite common method I'm sure. Openning these pages on there own will just induce errors and may reveal code through the error messages. This may not be a huge security problem as I have no sensitve stuff, but I would still like to be able to do it...

Through guessing and investigation, the solution seems to be to use the mod_rewrite module from within a .htaccess file. Here's what I've got, and it don't work:

RewriteEngine on

RewriteCond %{SCRIPT_FILENAME} !^.*/foldername/filename\.php.*$ [NC]

RewriteRule .*\.php http://domainname/folder/filename [R,NC]

I'm not sure if my web hosting gives me access to the mod_rewrite module...

I could also prevent errors from being displayed in PHP, or code the redirect within each individual PHP script, but I feel the .htaccess method is the best.


Cheers,
-D



jeremy.king
Jan 14, 2005, 08:55 PM
Why not just move your include files to a non-servable directory?

That way they will never be exposed to the internet, never be executed alone, and you will sleep better at night. I think this is more common practice. ;)

For example, one of my ISPs gives me a dir structure something like

/path/to/home
|
|--- cgi-bin
|
|--- logs
|
|--- www


I just added an includes directory off my home dir and that dir can never be reached since the webserver serves up from www (with a script alias pointing at cgi-bin). But I can include the files simply by using relative directory paths.