How can I change the URL from a PHP extension to a PNG extension? I am making an image generator for users to post their score on a test on forums. The main targeted forum, though, does not allow php extensions in images. How can I change this URL:
http://everythingtutorials.org/noobtest/buildimg.php?1=2816
to something like this:
http://everythingtutorials.org/noobtest/picture_2816.png
What you’re asking is done at the webserver level, before the request reaches PHP. You need to configure your webserver to send all requests for the desired URL format to the PHP script.
You don’t state what webserver you’re using. The following answer applies if you’re using Apache, which is arguably the most common webserver for powering PHP sites.
One way to do this is using the Apache mod_rewrite module.
mod_rewriteallows Apache to “rewrite” specific URLs to transform them into other URLs.The specific Apache directive you want are:
These should be placed into your Apache’s
<VirtualHost>configuration directive inhttpd.confor similar. If you don’t have access to the server’s configuration, you can try placing these in a.htaccessfile..htaccessfiles are per-directory configuration files (names, you guessed it,.htaccess) which contain Apache directives. Using a.htaccessfile is less efficient but may be required.To set up a RewiteRule using a
.htaccessfile, create a file/noobtest/.htaccesscontaining:Note that you don’t specify the directory here. Also, note that mod_rewrite must be loaded by your server already and you must have permission (via
AllowOverride) to override Apache’s configuration in this way. If either of those is not true and you can’t modify Apache’s configuration files, you’re out of luck.