简体   繁体   中英

How do I do URL rewriting in php?

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

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.

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:

    \nRewriteEngine on \nRewriteCond %{REQUEST_FILENAME} !-f \nRewriteRule !^index\\.php$ index.php [L] \n

    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:

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

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

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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