Hide Page Extensions (Like StackOverflow)

前端 未结 6 1335
故里飘歌
故里飘歌 2020-12-07 04:42

I want to hide page extensions like stackoverflow does. How does the following work?

http://stackoverflow.com/tags/foo
http://stackoverflow.com/tags/bar
         


        
6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-07 04:51

    There are a couple of ways to do it under Apache+PHP, but the essential principle is to make a set of URIs (perhaps all URIs, depending on your site, but you may want different scripts to handle different portions of the site) translate to a single PHP file, which is told what object the user has requested.

    The conceptually simplest way is to rewrite every URL to a script, which gets the URI through $_SERVER['REQUEST_URI'] and interprets it as it likes.

    The URI rewriting can be done with various methods including mod_rewrite, mod_alias and ErrorDocument (see Apache docs).

    Another way is to set up more complex URL rewriting (probably using mod_rewrite) to add the path as a GET variable.

    There is also the $_SERVER['PATH_INFO'] variable which is loaded with the non-existent portion of the path. This option requires little or no modification to Apache config files, but reduces the flexibility of your URLs a little.

提交回复
热议问题