简体   繁体   English

从/buildimg.php?1 = 2816到/picture_2816.png的PHP图片网址?

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

How can I change the URL from a PHP extension to a PNG extension? 如何将URL从PHP扩展名更改为PNG扩展名? 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. 但是,主要的目标论坛不允许在图像中使用php扩展。 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. 您要求的是在请求到达PHP之前在Web服务器级别完成的。 You need to configure your webserver to send all requests for the desired URL format to the PHP script. 您需要配置Web服务器以将所需的URL格式的所有请求发送到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,则可以使用以下答案,Apache可以说是支持PHP站点的最常用的Web服务器。

One way to do this is using the Apache mod_rewrite module . 一种方法是使用Apache mod_rewrite模块 mod_rewrite allows Apache to "rewrite" specific URLs to transform them into other URLs. mod_rewrite允许Apache“重写”特定的URL以将其转换为其他URL。

The specific Apache directive you want are: 您想要的特定Apache指令是:

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. 这些应放在httpd.conf或类似的Apache的<VirtualHost>配置指令中。 If you don't have access to the server's configuration, you can try placing these in a .htaccess file . 如果您无权访问服务器的配置,可以尝试将它们放在.htaccess文件中 .htaccess files are per-directory configuration files (names, you guessed it, .htaccess ) which contain Apache directives. .htaccess文件是包含Apache指令的每个目录配置文件(名称,您猜对了.htaccess )。 Using a .htaccess file is less efficient but may be required. 使用.htaccess文件效率较低,但可能需要。

To set up a RewiteRule using a .htaccess file, create a file /noobtest/.htaccess containing: 要使用.htaccess文件设置RewiteRule,请创建一个包含以下内容的文件/noobtest/.htaccess

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 . 另请注意, mod_rewrite必须已由您的服务器加载,您 必须具有权限(通过AllowOverride )以这种方式覆盖Apache的配置 If either of those is not true and you can't modify Apache's configuration files, you're out of luck. 如果其中任何一个不正确并且您无法修改Apache的配置文件,那么您就不走运了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM