htaccess rewrite url like Stack Overflow

后端 未结 2 1510
时光说笑
时光说笑 2021-01-14 20:24

Stack Overflow generates rewrite URLs,

so i need to know how i can do it like Stack Overflow?

http://stackoverflow.com/questions/9168364/how-to-rewri         


        
2条回答
  •  渐次进展
    2021-01-14 20:41

    .htaccess:

    
      Options +FollowSymLinks
      RewriteEngine On
    
      # Get rid of index.php
      RewriteCond %{REQUEST_URI} /index\.php
      RewriteRule (.*) index.php?rewrite=2 [L,QSA]
    
      # Rewrite all directory-looking urls
      RewriteCond %{REQUEST_URI} /$
      RewriteRule (.*) index.php?rewrite=1 [L,QSA]
    
      # Try to route missing files
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} public\/ [OR]
      RewriteCond %{REQUEST_FILENAME} \.(jpg|gif|png|ico|flv|htm|html|php|css|js)$
      RewriteRule . - [L]
    
      # If the file doesn't exist, rewrite to index
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ index.php?rewrite=1 [L,QSA]
    
    
    

    index.php:

    $url = $_SERVER['REQUEST_URI'];
    $url = explode("/",substr($url,1));
    

    Your links would be readable by PHP as follow:

    http://www.website.com/First/Second/Third

    $url[0] >> 'First'

    $url[1] >> 'Second'

    $url[2] >> 'Third'

提交回复
热议问题