php PHP中的URL映射?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/395650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:36:34  来源:igfitidea点击:

URL mapping in PHP?

phpapacherest

提问by cletus

I come from a Java background and with any servlets-based technology, it's trivial to map a range of URLs (eg /reports/, /secure/.do) to a specified servlet. Now I'm less familiar with PHP but I haven't yet seen anything that does quite the same thing with PHP (or mod_php). It's entirely possible that I'm missing something simple.

我来自 Java 背景并且使用任何基于 servlet 的技术,将一系列 URL(例如 /reports/ 、/secure/.do)映射到指定的 servlet是微不足道的。现在我对 PHP 不太熟悉,但我还没有看到任何与 PHP(或 mod_php)完全相同的东西。我完全有可能遗漏了一些简单的东西。

How do you do this in PHP?

你如何在 PHP 中做到这一点?

One of the reasons I want to do this is "one use" URLs. Now this can sorta be done with GET parameters (like an MD5 hash token) but I'm interested in URL mapping as a general solution to many problems.

我想要这样做的原因之一是“一次性”URL。现在这可以通过 GET 参数(如 MD5 哈希令牌)完成,但我对 URL 映射感兴趣,作为许多问题的通用解决方案。

Another big reason to use something like this is to have RESTful URLs.

使用这样的东西的另一个重要原因是拥有 RESTful URL。

回答by CMS

With Apache, you are able to setup URL Rewriting for your php pages with mod_rewrite, check this resources:

使用 Apache,您可以使用 mod_rewrite 为您的 php 页面设置 URL 重写,请查看以下资源:

回答by Eineki

Other than using mod_rewrite, as already reported you can do a little of magic with a simple trick.

除了使用 mod_rewrite 之外,正如已经报道的那样,您可以通过一个简单的技巧来实现一些魔法。

Put into the .htaccess a directive like this one

将这样的指令放入 .htaccess

<FilesMatch "^servlet$"> 
  ForceType application/x-httpd-php
</FilesMatch> 

substitute ^servlet$ with a regular expression of your choice (it will be the name of your dispatcher)

用您选择的正则表达式替换 ^servlet$(它将是您的调度员的名称)

The file servlet should be similar to this

文件 servlet 应该与此类似

<?php
  $data = explode('/',$HTTP_SERVER_VARS['PATH_INFO']); // $data[0] always empty
  $fileToInclude = $data[1].'.php';
  if (file_exists($data[1]) {
     $params=array_slice($data,2); // you can do here something more sophisticated
                                   // for example sanitize parameters or assemble 
                                   // an hash
     include ($fileToInclude);     //Think to this file as a servlet
  } else {
    // issue a 404 error, maybe one of the 500 series
  }
?>

The url can have the form: http://yoursite/servlet/reports/sales/2009you can also reach the form http://yoursite/reports/sales/2009plaiyng a little with the .htacces and the dispatcher.

该 url 可以具有以下形式:http://yoursite/servlet/reports/sales/2009您还可以 通过 .htacces 和调度程序访问表单http://yoursite/reports/sales/2009plaiyng。

This method has the advantage that mod_rewrite is not required as FilesMatch (1.3+) and ForceType (2.0+) are in the apache core

这种方法的优点是不需要 mod_rewrite,因为 FilesMatch (1.3+) 和 ForceType (2.0+) 在 apache 核心中

See for reference
http://httpd.apache.org/docs/2.2/mod/core.html#forcetype
http://httpd.apache.org/docs/2.2/mod/core.html#filesmatch
http://www.devarticles.com/c/a/Apache/Using-ForceType-For-Nicer-Page-URLs/1/

请参阅参考
http://httpd.apache.org/docs/2.2/mod/core.html#forcetype
http://httpd.apache.org/docs/2.2/mod/core.html#filesmatch
http://www .devarticles.com/c/a/Apache/Using-ForceType-For-Nicer-Page-URLs/1/

回答by null

A trick I use is the $ _ SERVER['PATH_INFO'] (I think you need Apache). This gets you all of the information after the PHP file extension.

我使用的一个技巧是 $_ SERVER['PATH_INFO'] (我认为你需要 Apache)。这将为您提供 PHP 文件扩展名后的所有信息。

http://www.example.com/page.php/rest/url

the $ _ SERVER['PATH_INFO'] will contain:

$ _ SERVER['PATH_INFO'] 将包含:

"/rest/url"

The page can then redirect, process some code or whatever based on that string.

然后页面可以重定向、处理一些代码或基于该字符串的任何内容。

This is good if you don't have access to the Apache Configs or .HTACCESS, Also if things change you can make page.php a directory name for static file placement. Oh and PHP doesn't need to have a .php extension. I've used .api or somesuch in the past.

如果您无权访问 Apache Configs 或 .HTACCESS,这很好,此外,如果情况发生变化,您可以将 page.php 设为静态文件放置的目录名称。哦,PHP 不需要有 .php 扩展名。我过去使用过 .api 或类似的东西。

http://www.example.com/worker.api/rest/url

That seems to work well for me.

这对我来说似乎很有效。

回答by Michael Borgwardt

Can't be done in PHP as such. Note that in Java, it's not the Servlets either that map URLs, but it's the configuration of the servlet container (e.g. Tomcat).

不能在 PHP 中完成。请注意,在 Java 中,映射 URL 的不是 Servlet,而是 servlet 容器(例如 Tomcat)的配置。

You can achieve similar results for PHP applications by using URL rewriting.

您可以通过使用URL 重写为 PHP 应用程序实现类似的结果

回答by Stephen Booher

This sounds like something that mod_rewrite would typically be used for (as a part of Apache, not PHP.) I have not heard of anything in PHP which would accomplish this.

这听起来像是 mod_rewrite 通常会用于的东西(作为 Apache 的一部分,而不是 PHP。)我还没有听说过 PHP 中有任何东西可以实现这一点。

回答by Rob Allen

Options for HTTP servers other than Apache:

Apache 以外的 HTTP 服务器的选项:

If you are using Lighttpd, then their mod_rewrite module has a completely different configuration syntax. See http://redmine.lighttpd.net/wiki/1/Docs:ModRewritefor details.

如果您使用的是 Lighttpd,那么它们的 mod_rewrite 模块具有完全不同的配置语法。有关详细信息,请参阅http://redmine.lighttpd.net/wiki/1/Docs:ModRewrite

If you are using IIS, then you need ISAPI_Rewrite (http://www.isapirewrite.com/) or the URL Rewrite Module (http://learn.iis.net/page.aspx/460/using-url-rewrite-module/).

如果您使用的是 IIS,那么您需要 ISAPI_Rewrite ( http://www.isapirewrite.com/) 或 URL 重写模块 ( http://learn.iis.net/page.aspx/460/using-url-rewrite-模块/)。

回答by ejunker

As others have mentioned you can use mod_rewrite to do this but also you could use a front controller design pattern which is what many of the web frameworks use. Take a look at Horde routesor Zend_Controller_Router_Rewritewhich can be used for MVC,

正如其他人提到的,您可以使用 mod_rewrite 来执行此操作,但您也可以使用许多 Web 框架使用的前端控制器设计模式。看看可用于 MVC 的Horde 路由Zend_Controller_Router_Rewrite