URL Rewrite HTACCESS GET


URL Rewrite HTACCESS GET

目前我正在使用

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^'.]+)$ $1.php [NC,L]

例如重写 http://localhost/products.php

重写到 http://localhost/products

我将如何添加,以便它也会将 http://localhost/products.php?category=all 重写为 http://localhost/products/all/

我并没有完全理解代码。谢谢!

您可以使用这些规则添加另一个带有查询字符串的 URL,以重定向到 SEO 友好 URL。还有一些额外的规则,以防止使用旧的URL样式。

RewriteEngine On
#redirect old url to pretty url
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}' /+(.+)'.php'?category=([^&'s]+)
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}' /+(.+)'.php
RewriteRule ^ /%1? [R=301,L]
#if request is for a real file or real directory do nothing.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
#internally redirect uri to php
RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?category=$2 [NC,L]
RewriteRule ^([^'.]+)/?$ $1.php [NC,L]

.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . 1.php [L]
</IfModule>

创建文件 1.php

#split the path by '/'
  $params     = split("/", $_SERVER['REQUEST_URI']);
    define("FILE_PHP",".php");
    define("ERROR_404","404.php");
    define("INCLUDE_ADMIN","index.php");
    class Loader{
     public function __autoload($params) {
         if(isset($params[1])==TRUE){ 
          if (file_exists($params[1].FILE_PHP)) { 
             if(is_set($params[2])==true)
              $category=$params[2];
              require_once $params[1].FILE_PHP; 
              return true; 
          } 
           require_once ERROR_404; 
          return false; 
         }else{
          require_once INCLUDE_ADMIN; 
          return TRUE; 
         }
    } 
    }
    $loader=new Loader();
    $loader->__autoload ($params);