Jump to content
xisto Community
vujsa

Need .htaccess Filesmatch Help sethandler properties and use

Recommended Posts

How would I get

http://forums.xisto.com/no_longer_exists/

to forward to

http://forums.xisto.com/no_longer_exists/

 

I recently found that you can set a php file to appear to be an image file or some other type of file.

 

Just place the following .htaccess code into the directory of the disguised script. In this case, we are disguising a PHP file as a png image file:

 

<FilesMatch "^.*\.png">   SetHandler application/x-httpd-php</FilesMatch>

 

The code above is used to make a PHP generated .png image appear to be just like any other .png image your site might contain.

 

Now, I could write a script for every PHP generated image I want to serve but that would take up a lot of disk space and could potentially result in hundres or thousands of scripts to be written. Additionally, why bother writing a script to generate an image if I have to do one for each image, the whole point of the script is to reduce my workload.

 

Any help here would be great.

 

vujsa

Share this post


Link to post
Share on other sites

Actually, what it looks to me like that piece of code does is say:
make the application that handles *.png files (SetHandler) the php compiler (application/x-httpd-php). So all that happens is that png files are automatically sent to the php interpreter. To have a php script return a png file, place this code at the end of it:

header("Content-type: image/png");imagepng($image);
where $image is the variable holding your image. This sets the return type of the script to a png file and then retunrs the image. You then call the script in a page using an <img src="http://www.blah.com/script.png; tag. If you want to have one script create multiple images, simply make a script that takes a variable from a get and use this kind of image tag: <img src="http://www.blah.com/script.png?image=foo; and have your script create a differenty image depending on the value of foo. I use this technique to create an image that contains random text for a signup validation script.

~Viz

Share this post


Link to post
Share on other sites

<FilesMatch "^.*\.png"> SetHandler application/x-httpd-php</FilesMatch>I don't like this above code. PNG image files are binary, PHP files are ASCII.What you are doing however,is making the extension of PNG act like PHP, so that means we could write PHP inside a PNG file and have it work just like a PHP page. We should not however do this.I see why you would do this, is with your next code:http://forums.xisto.com/no_longer_exists/ forward to http://forums.xisto.com/no_longer_exists/ should work on doing:http://forums.xisto.com/no_longer_exists/ extension should be PHP and handled by the PHP interpretter, not PNG and handled by the PHP interpretter.We could then call this image using <img src="images/banner.php?action=4736514" alt="banner" />in our banner script we would then $_GET['action'] refers to the image. We could just do If(isset($_GET['action'])) { $image = 'banner' . $_GET['action'] . '.png'; }That's not a safe way, just an example.We would then output the image correctly with the correct mime-type. Which vizsky has showed how it works.There's plenty of scripts available to do this, I may show one I created which randomised the images in a certain directory and displayed it, however I'm working on a better randomisation for it so that the images per user are always different until they've gone through the whole image list which is why I won't show it yet till I've finished it.Cheers,MC

Share this post


Link to post
Share on other sites

Okay, looking now I see that once again because I didn't know what I wanted, I didn't explain my question very well.

 

The most important part of the question is how to make the seen URL friendly and clean. I don't want the user to see that the image is generated on the fly or that there is any scripting used at all.

 

I found the following example .htaccess code sample:

# Nice looking URLs (no querystring)# domain.com/category-name-1/ to domain.com/categories.php?name=category-name-1RewriteRule ^([A-Za-z0-9-]+)/?$ categories.php?name=$1 [L]

 

This is very near the reg_exp I need to accomplish my goal, but I need some help with it.

 

I understand what you are trying to say mastercomputers but for the project, I'll need the url to appear to be an image file because it will not be allowed by the target application in many cases otherwise. I'm working on a dynamic graphical signature generator for users to place a signature image in the accounts on other forums.

 

It would work similar to the search engine friendly URL's here:

If you click http://forums.xisto.com/topic/86614-topic/?findpost=1064322612, then you see http://forums.xisto.com/topic/86614-topic/?findpost=1064322612 in the address bar but the file is actually here: http://forums.xisto.com/topic/86614-topic/?findpost=1064322612

 

We see links to html files but this site doesn't have any real html files on the server.

 

Hope this better explains what I am looking for.

 

vujsa

Share this post


Link to post
Share on other sites

If I understand correctly, you want a friendly <img src="images/banner1234.png" alt="banner" />Which really is a link to images/banner.php?action=1234Is that what you want?I don't have time to show you how we do the banner.php page, but it's similar to how I explained above.The .htaccess file may go something like this:RewriteEngine onRewriteRule ^banner([0-9]{1,})\.png$ banner.php?action=$1I hope this is something close to what you want, I just don't understand why you would have a banner.png?action=1234 file, because png is an image extension and should be used as images, not text files. Don't confuse the web browsers, since they understand the mime-type of .png file, yet to come across this, they will need to automatically detect it's type because it wouldn't appear correct to them.Cheers,MC

Share this post


Link to post
Share on other sites

This doesn't confuse the browser at all. The url points to a png image and that is exactly what is returned as a result. The mask (of sorts) doesn't take place until the server where the request for a png image is translated into the correct request for the php script that actually builds and returns the image. Your argument would be completely valid if the output was not a png format image. If I were using this toreturn text or html output, then this indeed would be an incorrect use of the mod_rewrite apach module. I am fairly sure that the browser will either give an error statement or will show nothing at all. Don't worry about showing me how to do the banner.php script, I've already got that research done. I understand how all of the variables will be passed, I just needed help with the url rewrite in the .htaccess.Thanks for taking the time to answer my question. You alway seem to come online just when I need you the most.The regular expression looks exactly like want I need, can't wait to try it out but it's so late here now. [/hr]That worked perfectly! That was exactly the result I was trying for. I only ran it with a test script but it pulled the the banner number out of the filename and displayed the number using an echo statement. [/hr]Thanks Again So Much. B)vujsa

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.