PHP Image URL from /buildimg.php?1=2816 to /picture_2816.png?

早过忘川 提交于 2019-12-10 20:17:57

问题


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

回答1:


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_rewrite allows Apache to "rewrite" specific URLs to transform them into other URLs.

The specific Apache directive you want are:

RewriteEngine on
RewriteRule ^/noobtest/picture_([0-9]+)\.png$ /noobtest/buildimg.php?id=$1 [L]

These should be placed into your Apache's <VirtualHost> configuration directive in httpd.conf or similar. If you don't have access to the server's configuration, you can try placing these in a .htaccess file. .htaccess files are per-directory configuration files (names, you guessed it, .htaccess) which contain Apache directives. Using a .htaccess file is less efficient but may be required.

To set up a RewiteRule using a .htaccess file, create a file /noobtest/.htaccess containing:

RewriteEngine on
RewriteRule ^picture_([0-9]+)\.png$ buildimg.php?id=$1 [L]

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.



来源:https://stackoverflow.com/questions/8590034/php-image-url-from-buildimg-php1-2816-to-picture-2816-png

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!