URL mapping in PHP?

前端 未结 7 1296
孤街浪徒
孤街浪徒 2020-12-05 01:05

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

相关标签:
7条回答
  • 2020-12-05 01:19

    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).

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

    0 讨论(0)
  • 2020-12-05 01:21

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

    Put into the .htaccess a directive like this one

    <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)

    The file servlet should be similar to this

    <?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/2009 you can also reach the form http://yoursite/reports/sales/2009 plaiyng a little with the .htacces and the dispatcher.

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

    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/

    0 讨论(0)
  • 2020-12-05 01:23

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

    • mod_rewrite: A Beginner's Guide to URL Rewriting
    • Module mod_rewrite
    • URL Rewriting Guide
    0 讨论(0)
  • 2020-12-05 01:23

    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.

    0 讨论(0)
  • 2020-12-05 01:27

    Options for HTTP servers other than Apache:

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

    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/).

    0 讨论(0)
  • 2020-12-05 01:36

    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.

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

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

    "/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.

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

    That seems to work well for me.

    0 讨论(0)
提交回复
热议问题