未应用RewriteRule [英] RewriteRule is not being applied
问题描述
我有以下规则:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule materia/([^/]+)/?$ article.php?slug=$1 [QSA,NC]
RewriteRule busca/?s=([^/]+) search.php?s=$1 [QSA,NC]
RewriteRule ^([^/]+)/?$ index.php?slug=$1 [QSA,NC]
</IfModule>
但是,当URL类似于 http://website时,第二个重写规则未应用.test/busca/?s = teste
But the second rewrite rules is not being applied when the URL is something like http://website.test/busca/?s=teste
htaccess测试人员: https://htaccess.madewithlove.be? share = 28e2b25d-f6ef-5faa-a5c6-a412a87d5523
htaccess tester: https://htaccess.madewithlove.be?share=28e2b25d-f6ef-5faa-a5c6-a412a87d5523
推荐答案
此处有多个错误:
-
要匹配查询字符串(
?s=xxx
),您需要在%{QUERY_STRING}
RewriteCond
范围仅用于下一个规则 .对于您而言,它只会检查第一条规则是否不是现有的文件夹/文件.
RewriteCond
scope is for very next rule only. In your case, it only checks if it's not an existing folder/file for the first rule.
在每个规则之后使用L
标志,除非您知道自己在做什么.
Use L
flag after each rule, unless you know what you're doing.
使用RewriteBase
或绝对路径.
QSA
标志.它所做的只是将查询字符串(在重写之前)附加到重写目标.
QSA
flag is not necessary when you rewrite manually the query string. All it does is to append the query string (before the rewrite) to the rewrite target.
多合一,这是您的规则的外观
All-in-one, here is how your rules should look like
RewriteEngine On
# Don't touch existing folders/files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^materia/([^/]+)/?$ /article.php?slug=$1 [L,NC]
RewriteCond %{QUERY_STRING} ^s= [NC]
RewriteRule ^busca/?$ /search.php [L,QSA,NC]
RewriteRule ^([^/]+)/?$ /index.php?slug=$1 [L]
这篇关于未应用RewriteRule的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!