PHP图像URL从/buildimg.php?1=2816到/picture_2816.png? [英] PHP Image URL from /buildimg.php?1=2816 to /picture_2816.png?

查看:49
本文介绍了PHP图像URL从/buildimg.php?1=2816到/picture_2816.png?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将URL从PHP扩展名更改为PNG扩展名?我正在为用户制作图像生成器,以便将其分数发布到论坛的测试中.但是,主要针对的论坛不允许在图像中使用php扩展名.如何更改此URL:

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

类似这样:

http://everythingtutorials.org/noobtest/picture_2816.png

推荐答案

您要问的是在请求到达PHP之前在Web服务器级别完成的.您需要配置您的网络服务器,以将对所需URL格式的所有请求发送到PHP脚本.

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.

您没有说明正在使用的Web服务器.如果您使用的是Apache(可以说是为PHP网站提供支持的最常见的网络服务器),则以下答案适用.

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.

一种方法是使用Apache mod_rewrite模块. mod_rewrite允许Apache重写"特定的URL,以将其转换为其他URL.

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.

您想要的特定Apache指令是:

The specific Apache directive you want are:

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

这些应该放在httpd.conf或类似版本中的Apache的<VirtualHost>配置指令中. 如果您无权访问服务器的配置,则可以尝试将其放置在.htaccess文件中. .htaccess文件是每个目录的配置文件(名称,您猜到了.htaccess),其中包含Apache指令.使用.htaccess文件的效率较低,但可能是必需的.

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.

要使用.htaccess文件设置RewiteRule,请创建包含以下内容的文件/noobtest/.htaccess:

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]

请注意,您无需在此处指定目录.另外,请注意 mod_rewrite必须已经由服务器加载,并且您必须具有通过这种方式覆盖Apache配置的权限(通过AllowOverride).如果这两个都不正确,并且您无法修改Apache的配置文件,那么您就不走运了.

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.

这篇关于PHP图像URL从/buildimg.php?1=2816到/picture_2816.png?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆