简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM