如何使用 .htaccess 为多个参数创建干净的 url?

How to create clean url using .htaccess for multiple parameters?

我尝试为我的网站创建干净的 URL。 所需格式为:site/category/num 示例 site/sport/3

类别和页面都是可选参数。所以我有三个规则。问题是规则三。它只在第一次工作。

用户进入站点 - 规则 1。确定。 用户导航并选择类别。规则 2... 好吧。 用户导航并想要第 2 页页面。 (规则 3)好的。 用户希望在第 3 页(规则 3)中看到更多项目错误..

我得到参数 site/category/category/3 并且导航失败。现在我不明白为什么我得到了两次类别。

我的 .htaccess 文件差不多可以用了。

RewriteEngine On

RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)$ /index.php?page= [L]

RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /index.php?category= [L]

RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9]+)$ index.php?category=&page= [NC,L]

试试:

RewriteEngine On
RewriteCond %{REQUEST_URI} (\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC,OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^([0-9]+)/?$ /index.php?page= [L]
RewriteRule ^([^/]+)/?$ /index.php?category= [L]
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9]+)/?$ /index.php?category=&page= [NC,L]