首页 > 解决方案 > 如何在 htaccess 和 mod-rewrite 上重写两个 GET 值

问题描述

我只想制作我发送的 GET 信息:

http://mywebsite.com/folder/index.php?category=category-alias-here http://mywebsite.com/folder/index.php?category=category-alias-here&page=5

在 URL 栏上重写为:

http://mywebsite.com/folder/category-alias-here/

http://mywebsite.com/folder/category-alias-here/5/

我不能。它很简单,我看到了几个类似的问题。在 stackoverflow 上测试了几个示例,它们都给了我 404 类别和两个页面。

这是我测试的代码:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?category=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?category=$1&page=$2 [L]

我知道这段代码对我的结构来说是错误的,我尝试了一些更改放置和删除 ([^/]+) 或将拳头 [L] 更改为 [N]。还有其他几件事。但没有运气。

请注意,整个网站都在一个真实的目录(/文件夹/)中,并且 .htaccess 也在那里(它不在 root 上),一旦我启动网站,一切都会在 public_html 上,但我不认为这是原因这个问题的。

是的,mod_rewrite 已开启。我实际上使用了一个 .htaccess,它可以在我放置的任何地方工作,并且它可以正确获取类别,但它有点混乱。

标签: .htaccessmod-rewrite

解决方案


找到了解决方案:

Options -Indexes

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteCond %{SCRIPT_FILENAME} !-f
 RewriteCond %{SCRIPT_FILENAME} !-d

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

 RewriteRule ^([a-z0-9-]+)\/$ index.php?category=$1&page=$2 [NC,L]

推荐阅读