Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...