.htaccess隐藏从url获取param并修改url

时间:2017-09-26 09:52:33

标签: .htaccess

现在我有以下网址 http://example.com/products/?category=clothes 我的主逻辑依赖于从url接收的get参数。 我有什么方法可以改变结构 http://example.com/products/clothes,还有带衣服的类别参数吗? 我尝试使用htaccess - 我试过这个,但它没有用。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^products/([^/]+)$ products/$1/?category=$1 [L]

1 个答案:

答案 0 :(得分:0)

您可以使用.htaccess

中的以下内容实现这一目标
RewriteEngine On
RewriteCond %{QUERY_STRING} category=(.+) [NC]
RewriteRule ^(.*)$ http://example.com/products/%1? [R=301,NC,L]

这会留下您的网址:http://example.com/products/clothes

我们在这里做的是抓取变量category=,并使用%1抓取与之关联的任何内容并将其放在网址的末尾。

然后,我们使用?停止原始查询,例如,?category=clothes显示在网址的末尾。

确保在>测试之前清除缓存