.htaccess:如何编写匹配url的规则,然后将url与参数匹配

时间:2021-07-16 10:37:57

I am newbie to htaccess rules, I want to make a rule for redirecting these URLs:

我是htaccess规则的新手,我想制定一个重定向这些URL的规则:

RewriteRule ^news$ index.php?r=news [QSA]
RewriteRule ^news/$ index.php?r=news [QSA]

the news url is an index page of some articles, so to access an article I wrote these rules:

新闻网址是一些文章的索引页面,所以要访问一篇文章,我写了这些规则:

RewriteRule ^news/([/s/S])$ index.php?r=news/p&id=$1 [QSA]
RewriteRule ^news/([/s/S])/$ index.php?r=news/p&id=$1 [QSA]

the first rule (for the news index) is working correctly, but the other rules doesn't work, what is the problem?

第一条规则(对于新闻索引)工作正常,但其他规则不起作用,问题是什么?

1 个解决方案

#1


2  

You must have meant [\s\S]* (= zero or more characters including a newline), not [/s/S]. It seems to me the newlines should not be present in URLs, that is why you can safely use .* instead.

你的意思是[\ s \ S] *(=包含换行符的零个或多个字符),而不是[/ s / S]。在我看来,新行不应该出现在URL中,这就是为什么你可以安全地使用。*。

Also, the first rule will match what the second rule is supposed to match (as the .* is matching any characters but a newline, it will match the final /, too), thus you need swap them.

此外,第一个规则将匹配第二个规则应该匹配的内容(因为。*匹配任何字符而不是换行符,它也将匹配最终的/),因此您需要交换它们。

#1


2  

You must have meant [\s\S]* (= zero or more characters including a newline), not [/s/S]. It seems to me the newlines should not be present in URLs, that is why you can safely use .* instead.

你的意思是[\ s \ S] *(=包含换行符的零个或多个字符),而不是[/ s / S]。在我看来,新行不应该出现在URL中,这就是为什么你可以安全地使用。*。

Also, the first rule will match what the second rule is supposed to match (as the .* is matching any characters but a newline, it will match the final /, too), thus you need swap them.

此外,第一个规则将匹配第二个规则应该匹配的内容(因为。*匹配任何字符而不是换行符,它也将匹配最终的/),因此您需要交换它们。