开发者

How to make a PHP GET Request method like this "http://somesite.com/games/213123"?

Hi guys I'm wondering how to make this kind of requests to the server I looked at many sites and they use this technique. For example gametrailers.com => http://www.gametrailers.com/video/level-six-diablo-iii/**721239开发者_JAVA百科**. I know that a GET request is made by using parameters like this: http://somesite.com/page.php?param=1. So how to make it like gametrailers. I hope you understand my question and I doubt that "721239" is a folder on the server with an index page inside of it.


You need to create a file placed in the folder near your script with name .htaccess

In this file you need to define rewriting rules. The contents of the file are:

RewriteEngine on 
RewriteRule ^games/(.*)$ games.php?id=$1 [L]

In this case http://somesite.com/games/213123 will be transformed into http://somesite.com/games.php?id=213123


The more convinient way is to do url rewriting. (wiki)

For example, you can have a .htaccess like this, well explained in this guide from symfony:

<IfModule mod_rewrite.c>
  RewriteEngine On

  # we skip all files with .something
  RewriteCond %{REQUEST_URI} \..+$
  RewriteCond %{REQUEST_URI} !\.html$
  RewriteRule .* - [L]

  # we check if the .html version is here (caching)
  RewriteRule ^$ index.html [QSA]
  RewriteRule ^([^.]+)$ $1.html [QSA]
  RewriteCond %{REQUEST_FILENAME} !-f

  # no, so we redirect to our front web controller
  RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>


You can achieve this using MultiViews in Apache2 (http://httpd.apache.org/docs/2.0/content-negotiation.html).

MultiViews (when enabled) instructs Apache to build a parameter map when a resource doesn't exist, so for www.foo.com/app/game/14, if www.foo.com/app.php exists and app/game and app/game 14 don't, it can be set up to translate that to www.foo.com/app.php?type=game&article=14

Some people also use mod_rewrite, but I'm not sure that's the preferred approach.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜