简体   繁体   English

为php脚本创建RESTful网址

[英]Create a RESTful url for a php script

I have a php script on a webserver. 我在网络服务器上有一个php脚本。 Currently, the script is being accessed as http://www.mydomain.com/scriptname.php . 当前,该脚本可以通过http://www.mydomain.com/scriptname.php Is there a way i can create a user friendly url for accessing this script, something like http://www.mydomain.com/appname, so when this url is called it invokes the php script ? 有没有一种方法可以创建用于访问此脚本的用户友好的URL,例如http://www.mydomain.com/appname,因此当调用此URL时,它将调用php脚本?

Please help. 请帮忙。 Thank You 谢谢

You want mod_rewrite if you're using Apache HTTPD: http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html 如果您使用的是Apache HTTPD,则需要mod_rewrite: http : //httpd.apache.org/docs/1.3/mod/mod_rewrite.html

If you're using a different web server, it may have something similar (lighttpd has a similar functionality builtin). 如果您使用的是其他Web服务器,则它可能具有类似的功能(lighttpd内置了类似的功能)。

Once it's enabled, you can use something like this in your .htaccess file to rewrite appname to scriptname.php 启用后,您可以在.htaccess文件中使用类似的内容将appname重写为scriptname.php

RewriteEngine on
RewriteRule ^appname$ scriptname.php

If you don't have access to your apache/lighttpd configuration file a little hack that may work is putting the script in http://www.mydomain.com/appname/index.php ; 如果您无权访问apache / lighttpd配置文件,则可能有一些可行的技巧将脚本放在http://www.mydomain.com/appname/index.php http://www.mydomain.com/appname/ will then probably work. http://www.mydomain.com/appname/可能会起作用。

Using the default settings of most PHP hosts, you can put the file "index.php" inside your folder http://www.mydomain.com/appname , then fill the index.php with next code: 使用大多数PHP主机的默认设置,可以将文件“ index.php”放在文件夹http://www.mydomain.com/appname中 ,然后用下一个代码填充index.php:

<?php
header("location: http://www.mydomain.com/scriptname.php");
?>

That would do it. 那样就可以了。

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

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