Creating dynamic URLs in htaccess

前端 未结 3 1621
遇见更好的自我
遇见更好的自我 2020-12-18 08:40

I\'m trying to write an .htaccess file that will make my URLs more attractive to search engines. I know basically how to do this, but I\'m wondering how I could do this dyna

3条回答
  •  悲&欢浪女
    2020-12-18 09:27

    One way to do it, would be just like most mvc frameworks. You can redirect all your pages to the same index.php file, and you use your script to determine which page to load.

    .htaccess

    
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php [L]
    
    

    and your php file will have a script like this one:

      // get the url
      $uri = (isset($_SERVER['REQUEST_URI']))?$_SERVER['REQUEST_URI']: false;
      $query = (isset($_SERVER['QUERY_STRING']))?$_SERVER['QUERY_STRING']: '';
      $url = str_replace($query,'',$uri); // you can edit this part to do something with the query
      $arr = explode('/',$url);
      array_shift($arr);
    
      // get the correct page to display
      $controller =!empty($arr[0])?$arr[0]:'home'; // $arr[0] could be product/ 
      $action = isset($arr[1]) && !empty($arr[1])?$arr[1]:'index'; // $arr[1] can be product-title
        }
    

    of course you will have to work this code to fashion your application

    I hope this helps

提交回复
热议问题