marcelo2605
marcelo2605

Reputation: 2794

RewriteRule is not being applied

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

Answers (1)

Justin Iurman
Justin Iurman

Reputation: 19016

Several mistakes here:

  1. to match a query string (?s=xxx), you need to use a RewriteCond on %{QUERY_STRING}

  2. 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.

  3. Use L flag after each rule, unless you know what you're doing.

  4. Use a RewriteBase or absolute paths.

  5. 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

Related Questions