How to rewrite SEO friendly url's like stackoverflow

后端 未结 5 1566
梦毁少年i
梦毁少年i 2020-12-16 08:21

If you go to a url like the following: http://stackoverflow.com/questions/9155602/ the address bar will be updated to http://stackoverflow.com/questions/9

5条回答
  •  既然无缘
    2020-12-16 08:36

    if i do a

    curl -I http://stackoverflow.com/questions/9155602/
    

    i actually get a hard redirect:

    HTTP/1.1 301 Moved Permanently
    Cache-Control: public, max-age=60
    Content-Length: 0
    Content-Type: text/html
    Expires: Mon, 06 Feb 2012 22:43:27 GMT
    Last-Modified: Mon, 06 Feb 2012 22:42:27 GMT
    Location: /questions/9155602/redirect-existing-file-to-different-url-using-mod-rewrite
    Vary: *
    Date: Mon, 06 Feb 2012 22:42:27 GMT
    

    often these setups contain a short .htaccess, forcing all requests to go through a front controller, or a main index.php file. this file checks the $_SERVER['PATH_INFO'] variable and decides what content to pull in and render.

    this is a simple .htaccess example (actually just copied from a vanilla wordpress install):

    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    

    in this example, with the hard redirect, they could have an "else" in the index.php, making lookups and redirecting correctly if no content can be found based on the last piece of PATH_INFO.

提交回复
热议问题