Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ForceType with Rewrite

Posted on 2004-05-01
13
Medium Priority
?
1,045 Views
Last Modified: 2008-02-01
Hi,

I'm trying to set up a dynamic open source site, and I have the following idea:

For any page root:/dir/file.php, going to root:/txt/dir/file.php will show the source code without running it, i.e. a rewrite triggers a ForceType to text/plain.

Individually, I could handle each of these, but I don't know how to do them together.

Don't worry specifically about images etc being affected by the rewrite - users should never try /txt/path/to.jpg and if they do it'll be no benefit to them.
I also hide passwords and intentionally hidden data in .htaccess protected files so I don't think people will be able to steal anything sensitive.

Could you also tell me if this isn't posible, or even should be but you don't know how. That way I can stop wasting time looking for something that can't be achieved and get on with more vital areas.

Thank you,

_Blue
0
Comment
Question by:eeBlueShadow
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 14

Expert Comment

by:cracky
ID: 10970242
This can be done.

If you are happy to keep the same files in 2 locations, then this can be done without a Rewrite rule. Since ForceType works on a per-directory or location basis.

Assume you have a directory in your site called /source that holds exact duplicates of the directory structure of your root directory.

You would then place the following in your /source/.htaccess file:

ForceType application/x-httpd-php-source

This would then give you a syntax-highlighted source code for anything in /source/* no matter what the extension.

I'm sure you are aware, but make sure nothing sensitive is placed in this directory.

I tested this same directive with a RewriteRule, but it causes an internal server error if the files are not phisically in the specified ForceType directory. It might work with a little tweaking, but I can't get it to work unless the files are duplicated physically in the /source directory. If I try this in a .htaccess in the root dir:

RewriteEngine on
RewriteRule ^/source/(.*) /$1
<Location /source>
    ForceType application/x-httpd-php-source
</Location>

It fails. Since it is trying to force type for files that are not actually there.

Does this make sense?
0
 
LVL 14

Expert Comment

by:cracky
ID: 10970272
I just checked the Apache docs and it turns out that <Location> can only be used in config, not in .htaccess.

So, technically speaking, you should be able to set this at config level without having to copy the files to 2 locations, since the <Location> directive processes the URL, not the file system.

This is just a guess and may or may not work in testing.
0
 
LVL 10

Author Comment

by:eeBlueShadow
ID: 10970943
I didn't really want to copy files to two locations, I kinda wanted it to be automatic - when I update the code I'd like the source to update as well...

If you can give me more advice on the location method, that'd be much appreciated

_Blue
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 14

Expert Comment

by:cracky
ID: 10970960
I can only see this working if you have access to the httpd.conf file on your server. Is this the case? Are you using a virtual host or a dedicated server?
0
 
LVL 10

Author Comment

by:eeBlueShadow
ID: 10970979
Yeah, it's ok :)
0
 
LVL 14

Expert Comment

by:cracky
ID: 10971094
OK,

The Location directive processes the URL used to access the site, so to add the ForceType on a Location basis, you need to do this in your server's root config section (not .htaccess). Look for it in your httpd.conf file.

<Location /source/*>
    ForceType application/x-httpd-php-source
</Location>

For more info: http://httpd.apache.org/docs/mod/core.html#location

You will also need to apply a RewriteRule in your root Directory:

<Directory "/Your/Document/Root">

    Options Indexes FollowSymLinks MultiViews

    AllowOverride None

    Order allow,deny
    Allow from all

    RewriteEngine on
    RewriteRule ^/source/(.*) /$1

</Directory>

Be aware that the order in which these requests are processed is relevant and Directory directives are evaluated before Location directives. It is for this reason that what I am saying is only theoretical and might break, since your Location directive using ForceType takes effect after the RewriteRule that makes the request for the file.

I don't have time to test it on my server, but hopefully this will get you going in the right direction and give you a grasp of what I am proposing.
0
 
LVL 10

Author Comment

by:eeBlueShadow
ID: 10973531
Ok, I've had a bit of a fiddle and I still can't see why it isn't working.

The RewriteRule is fine (didn't need the initial / but thats irrelevant)

The ForceType doesn't appear to do anything in a <Location>.

I found out that there is a [T=mime/type] flag for RewriteRule to force a MIME type onto a rewrite, but that also isn't doing anything either.

What could there be which is overriding my ForceType? A ForceType on <Files *> in an .htaccess works, what other in-between stages can I try to find out where the problem is? (I don't know anything about the order rules are processed and can't make much sense of the docs)

Cheers for the help,
_Blue
0
 
LVL 10

Expert Comment

by:Mercantilum
ID: 10975510
It maybe difficult to get this working through forcetype and rewriterule when you want to access a cgi-bin file.

What I suggest is

/www/cgi-bin/mycgi   being your cgi dir
/www/docs/mydoc     being your documents dir

Simply do as root

   cd /www/docs/mydoc
   ln -s /www/cgi-bin/mycgi source

Ensure you have the
   Options +FollowSymLinks
for the docs directory to allow links following.

Try to access your cgi dir  
    http://www.x.com/source/myscript 
and you get the source of your script.

( in case of error(s), have a look to your /var/log/httpd/error_log )
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 1500 total points
ID: 10978101
(didn't follow/test all suggestions so far)

You don't need 2 directories or 2 sites, just a rewrite rule.
Best is you use a cgi script to display your source, this cgi gets the file to be displayed as parameter.
Also don't forget to add [PT] to the rewrite rule.

something like:

   RewriteCond  %{REQUEST_METHOD} ^(GET|POST)$
   RewriteCond  %{QUERY_STRING}  !^SHOW$
   RewriteRule  ^(.+)$           /cgi-bin/show.pl?$1 [PT]

where show.pl is (just an example, to be improved in many ways):
#!/usr/bin/perl
open F,$ENV{QUERY_STRING};print <F>;exit 0;
0
 
LVL 10

Expert Comment

by:Mercantilum
ID: 10978605
Just a clarification in my solution above (ln -s) there is still only one directory, you just set a link to the php scripts data
0
 
LVL 10

Author Comment

by:eeBlueShadow
ID: 10978979
ahoffmann, that's a good idea and I don't know why I didn't think of it before. It's definitely worth some points, but I'd like to find out why the other way doesn't work first.

Mercantilum, I don't have a cgi-bin, I thought of of the points of Apache's MIME type system was that you didn't need a separate directory for your executable files. (I saw a page somewhere about this but I can't find it now). Anyhow, my PHP files are intermingled with my HTMLs and my JPGs and I'd like to keep the directory structure as it is now.

Like I say, the biggest clue I have now is that the

RewriteRule ^/source/(.*) /$1 [T=application/x-httpd-php-source]

does the rewrite, but the type change doesn't work. I do get the following line in my rewrite.log though:

remember [...] to have MIME-type 'application/x-httpd-php-source'

and I don't know whats stopping it.

Thanks for your help so far,
_Blue
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 10979020
short time ago I also struggled with [T=blablabla], and came up with [PT], nothing else worked
can't say that it's a bug or alike, just didn't dig deeper 'cause [PT] did what I nedded :-|
0
 
LVL 10

Author Comment

by:eeBlueShadow
ID: 11000281
I couldn't find any way to get the Apache config working, seems it's an IE problem - IE tries to be too greedy.

The script solution worked, but it isn't perfect as it doesn't like URLs that don't match the local directory structure.

Thanks,
_Blue
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As Wikipedia explains 'robots.txt' as -- the robot exclusion standard, also known as the Robots Exclusion Protocol or robots.txt protocol, is a convention to prevent cooperating web spiders and other web robots from accessing all or part of a websit…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question