user2348668
user2348668

Reputation: 768

need help rewriting URL with htaccess

I'm trying to send queries that look like this:

example.org/search?q=search+query

to

search.pl?q=search+query

and all the other requests to index.php, this is how my htaccess looks like (not working)

RewriteEngine on
RewriteRule ^search\?q\=(.*)$ /cgi-bin/search.pl?q=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

This sends me to the index for some reason.

Upvotes: 0

Views: 49

Answers (1)

Mike Brant
Mike Brant

Reputation: 71422

The query string is not part of what is evaluated in a RewriteRule, so you need to remove that from the match condition in the RewriteRule like this:

RewriteRule ^/?search$ /cgi-bin/search.pl [L,QSA]

Note you don't need to use a backreference ($1) in the redirect as the QSA flag will append the existing query string to the URI as is.

Upvotes: 1

Related Questions