LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   How to setup Apache to redirect all requests under a certain path to a script? (https://www.linuxquestions.org/questions/linux-server-73/how-to-setup-apache-to-redirect-all-requests-under-a-certain-path-to-a-script-929491/)

fmillion 02-15-2012 10:05 AM

How to setup Apache to redirect all requests under a certain path to a script?
 
I know I've seen something exactly like this done on Mediawiki, and I'm curious how they do it.

I want to implement a file browsing script on my web server. Basically I want to be able to have a URL like this:

http://www.mysite.com/browse/path/in/the/filesystem

So, basically I want a PHP script to run anytime anyone accesses something under the "browse" directory, that will be passed the URL. (example: accessing the above URL would present a customized view of /path/in/the/filesystem. (of course I would prepend a "virtual root" directory on the path so as to jail the script into a certain directory.)

The goal obviously is to be able to just create a new folder in the filesystem, then be able to access it by going to http://www.mysite.com/browse/path/to/my/new/folder.

I do want to have the script execute EVEN IF the folder doesn't exist, because 1) I might want to create some special cases, and 2) I want the script to be able to present its own error messages. (Again, Mediawiki does that - when you request an unknown article it asks you if you want to create it.)

I know I can easily get the URL from PHP and extract the necessary parts. The only question I have is how do I setup Apache to direct all requests under a certain directory to a singular script?

As I said, Mediawiki does things like:

http://en.wikipedia.org/wiki/My_article

So I'm guessing there's a single script that runs, in which they extract the article title from the URL and go from there.

Just curious if anyone knows how they do that.

Thanks

FM

Doc CPU 02-16-2012 03:42 AM

Hi there,

Quote:

Originally Posted by fmillion (Post 4603046)
I want to implement a file browsing script on my web server. Basically I want to be able to have a URL like this:

http://www.mysite.com/browse/path/in/the/filesystem

So, basically I want a PHP script to run anytime anyone accesses something under the "browse" directory, that will be passed the URL. (example: accessing the above URL would present a customized view of /path/in/the/filesystem. (of course I would prepend a "virtual root" directory on the path so as to jail the script into a certain directory.)

I can think of two different ways of doing that. Both require that you are allowed to use a .htaccess configuration file.
  1. Just make sure that /browse (without the extension) is your PHP script. You'll have to tell Apache to parse it as a script despite not having a .php extension; that can be achieved with
    Code:

    <Files browse>
    ForceType application/x-httpd-php
    </Files>

    in a .htaccess configuration file (make sure that in *your* server setup, application/x-httpd-php is really the internal MIME type for PHP scripts). With that setup, your script is invoked any time a resource is requested that has /browse as the first part, while the rest of the request URL is passed to your script as $_SERVER['PATH_INFO'].
  2. Use mod_rewrite to internally redirect all requests to /browse and below to one specific script and pass the rest of the request URL as a URL parameter.
    Code:

    RewriteEngine on
    RewriteRule ^/browse(.*)$ /filebrowser.php?path=$1

    That would redirect all /browse requests to /filebrowser.php, and pass the rest of the request URL as $_GET['path'] to the script.

[X] Doc CPU


All times are GMT -5. The time now is 11:59 PM.