简体   繁体   English

需要帮助使用htaccess重写URL

[英]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) 以及对index.php的所有其他请求,这就是我的htaccess的样子(不起作用)

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中评估的内容的一部分,因此您需要像这样在RewriteRule中的匹配条件中将其删除:

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. 注意,您不需要在重定向中使用向后引用( $1 ),因为QSA标志会将现有查询字符串原样追加到URI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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