LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 02-15-2012, 10:05 AM   #1
fmillion
Member
 
Registered: Nov 2006
Posts: 93

Rep: Reputation: 27
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
 
Old 02-16-2012, 03:42 AM   #2
Doc CPU
Senior Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Hi there,

Quote:
Originally Posted by fmillion View Post
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

Last edited by Doc CPU; 02-16-2012 at 05:09 AM. Reason: Typo in code sample
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache server simultaneous requests time out while PHP script is running ilnli Linux - Server 2 04-02-2011 09:00 AM
Redirect https apache requests to folder not in apache root bax Linux - Enterprise 4 04-12-2010 01:16 PM
Redirect http requests angle2009 Linux - Networking 6 12-18-2009 05:08 PM
Redirect path in csh script ling_ss Linux - Newbie 6 03-15-2009 12:34 PM
Redirect SMTP Requests ALInux Linux - Networking 2 08-03-2006 07:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 02:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration