Reputation: 2794
I have theses rules:
<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>
But the second rewrite rules is not being applied when the URL is something like http://website.test/busca/?s=teste
htaccess tester: https://htaccess.madewithlove.be?share=28e2b25d-f6ef-5faa-a5c6-a412a87d5523
Upvotes: 0
Views: 47
Reputation: 19016
Several mistakes here:
to match a query string (?s=xxx
), you need to use a RewriteCond
on %{QUERY_STRING}
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.
Use L
flag after each rule, unless you know what you're doing.
Use a RewriteBase
or absolute paths.
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]
Upvotes: 1