Apache – query string rewrite rule appends condition to URL

apache-2.2mod-rewritequeryregex

In trying to rewrite

site-search.html?searchword=search%20term 

as

advanced-search#q=search%20term

I always end up with advanced-search#q=%3Fsearchword%3search%20term

The %3Fsearchword%3 portion is mysteriously appended to the generated URL right before the search term.

The rule I have in place look as follows:

RewriteCond %{REQUEST_URI}  ^/site-search\.html$
RewriteCond %{QUERY_STRING} ^searchword=(.*)$
RewriteRule ^(.*)$ http://www.example.com/advanced-search#q=%1 [R=301,L,NE]

It is worth mentioning that it makes no difference what I put after "q=".
The same error or a slight variation of to will happen regardless.

Best Answer

RewriteRule's %1 comes from the same RewriteRule's (.*) condition, and you match the query string there together with the URI itself.

Solution 1: Move or copy your pattern in the second RewriteCond into the RewriteRule's condition.

Based on the coments, and an assumption what would you like to reach, somehow like this:

RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$
RewriteCond %{REQUEST_URI}  ^/site-search\.html$
RewriteRule [?&]searchword=([^&]*) http://www.domain.com/advanced-search#q=%1 [R=301,L,NE]

Solution 2: explore RewriteRule's the [QSA] flag, which appends the entire query string to the resulting uri for you. (not for Apache 2.2)

Related Topic