简体   繁体   English

如何为此编写htaccess规则

[英]How to write an htaccess rule for this

I am new to htaccess rules and i am finding it difficult to write a htaccess rules to convert 我是htaccess规则的新手,我发现很难编写htaccess规则进行转换

http://www.frontieragturf.com/category.php?cat_id=1 http://www.frontieragturf.com/category.php?cat_id=1
to
http://www.frontieragturf.com/category/1 http://www.frontieragturf.com/category/1

I have tried these rules but that didn't work. 我已经尝试了这些规则,但是没有用。

.htaccess remove query string, keeping SEO style url .htaccess删除查询字符串,保留SEO样式网址
Customize Url or Hide Query String using .htaccess 使用.htaccess自定义网址或隐藏查询字符串

RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} "cat_id=" [NC]
RewriteRule (.*)  /$1? [R=302,L]

I have write this but its remove whole query string including 1 我已经写了这个,但是它删除了整个查询字符串,包括1
like this http://www.frontieragturf.com/category.php 像这样http://www.frontieragturf.com/category.php

RewriteEngine On
RewriteCond %{REQUEST_URI} ^category\.php$
RewriteCond %{QUERY_STRING} ^cat_id=(\d+)$ [NC]
RewriteRule .* /category/%1? [R=302,L]

Here, %1 refers to the first matching group of the previous RewriteCond , while $1 would refer to the first matching group from this RewriteRule , like in RewriteRule (.*) /category/$1 . 在这里, %1引用先前RewriteCond的第一个匹配组,而$ 1引用此RewriteRule的第一个匹配组,就像RewriteRule (.*) /category/$1 The manual says: 手册说:

RewriteCond backreferences: These are backreferences of the form %N (0 <= N <= 9). RewriteCond反向引用:这些反向引用的形式为%N(0 <= N <= 9)。 %1 to %9 provide access to the grouped parts (again, in parentheses) of the pattern, from the last matched RewriteCond in the current set of conditions. 从%1到%9,可以从当前条件集中最后匹配的RewriteCond访问模式的分组部分(再次用括号括起来)。 %0 provides access to the whole string matched by that pattern. %0提供对该模式匹配的整个字符串的访问。

The last ? 最后? kills the original query string. 杀死原始查询字符串。 Without it, it would be repeated for the redirected query: it would be /category/1?cat_id=1 , while the OP needs /category/1 . 如果没有它,将对重定向查询重复该操作:它将为/category/1?cat_id=1 ,而OP需要/category/1

Place this code in your DOCUMENT_ROOT/.htaccess file: 将此代码放在您的DOCUMENT_ROOT/.htaccess文件中:

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} \s/+category\.php\?cat_id=([^\s&]+) [NC]
RewriteRule ^ category/%1? [R=302,L]

RewriteRule ^category/([^/.]+)/?$ category.php?cat_id=$1 [L,QSA,NC]

Change 302 to 301 once you verify it is working for you. 一旦确认它对您有用,请将302更改为301

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM