How do I do URL rewriting in php?

2019-01-07 00:28发布

问题:

This is a double question: The correct answer will go to "How you do it in PHP" explaining if there is any advantage goes also counts if possible.

I'm just curious because I really don't know and I see it a lot in webpages.

Edit: I don't know the technical name, but for example here on Stackoverflow: "http://stackoverflow.com/posts/edit/522452" is what I mean as "folders" (a term previously used in the title of the question).

回答1:

If you're referring to /posts/edit/522452-style URLs as opposed to /posts.asp?action=edit&postid=522452-style URLs (or whatever it translates to on the back end), this is typically done through a URL rewriter, such as mod_rewrite. A rule for that URL might look like this:

RewriteRule ^/posts/(\w+)/(\d+) /posts.asp?action=\1&postid=\2

The two primary advantages to this kind of URL are that:

  1. "Folder"-type URLs are easier for people to type and to remember.
  2. The page "looks like" a page to HTTP proxies. Traditionally, proxies don't cache pages with parameters, as they don't represent separate content and change too frequently (think search results). Using "folder-style" URLs allows them to be cached normally.

In PHP, you can then access these options via $_GET['action'] and $_GET['postid'], exactly as if the browser had asked for the rewritten form.



回答2:

To use such URLs you have to do three steps:

  1. Tell you webserver, that those requests should redirected to your PHP script. With Apache you can use the mod_rewrite module to do so:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule !^index\.php$ index.php [L]
    

    This directives redirect every request, that does not match a file in the filesystem, to index.php.

  2. Get your script to parse those URLs:

    $_SERVER['REQUEST_URI_PATH'] = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
    $segments = explode('/', trim($_SERVER['REQUEST_URI_PATH'], '/'));
    var_dump($segments);
    

    This is probably the easiest way to parse the URL path and get each path segment.

  3. Use this URLs in your output. (trivial)



回答3:

A part for SEO purposes, caching and readability, as others have pointed out, I would like to add that folder-style parameters are also an incentive for users to "play around" with parameters. For instance here on Stackoverflow it is often tempting to edit the url by hand when filtering by tags, instead of looking for the appropriate button and clicking on it.



回答4:

There's a few reasons, but principally:

  • SEO purposes. Search engines usually ignore anything after the '?' question mark sign, thus leaving you with unindexes pages, or even worse, no indexing at all.

  • Readibility:

    www.somesite.com/products/devices/keyboards/20,asc
    www.somesite.com/products/devices/keyboards/20,asc.html
    

    looks better than:

    www.somesite.com?show=products&cat=devices&sub_cat=keyboards&sort=asc&items=20
    
  • Caching.

For info on how to do this in Apache: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html



回答5:

The advantage is readability and possibly a better placement in search engines.

You can achieve this via mod_rewrite if you're using Apache. In PHP, you can also look at the predefined globals - I think $_SERVER['REQUEST_URI'] works, but there might be a better one around.

When using plain PHP without rewriting, you can also drop the .php from the script's filename if you configure your server appropriately and thus still get 'nice' URLs.