Home > Blockchain >  Rewrite that appends a query string is not firing?
Rewrite that appends a query string is not firing?

Time:10-11

I'd like to perform internal redirect

From /search/lalafa to /search/lalafa?post_type=review

The rule is

RewriteRule ^/search/([^/] )$ /search/$1?post_type=review [L]

Seems correct but it doesn't seem to be matching:
https://htaccess.madewithlove.com/?share=c2ce1c7f-60be-40c8-9f0b-58ffc9eeba40

CodePudding user response:

RewriteRule ^/search/([^/] )$ /search/$1?post_type=review [L]

This will fail for two reasons:

  1. The URL-path matched by the RewriteRule pattern in .htaccess does not include the slash prefix, so this rule never matches. It should be ^search/([^/] )$.

  2. If it did match it would create a rewrite-loop (500 Internal Server Error) since you are rewriting to the same URL-path and not checking the query string.

Try the following instead:

RewriteCond %{QUERY_STRING} !=post_type=review
RewriteRule ^search/[^/] $ $0?post_type=review [L]

The $0 backreference contains the full URL-path, as matched by the RewriteRule pattern (NB: no slash prefix). The preceding condition checks that the query string is not already equal to post_type=review, thus preventing a rewrite-loop.

  • Related