1

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.

dawud
  • 15,096
  • 3
  • 42
  • 61
manchine
  • 51
  • 1
  • 7

2 Answers2

1

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)

asdmin
  • 2,050
  • 17
  • 28
  • 1
    The QSA flag is not supported under Apache 2.2, the version I am using. – manchine Jul 14 '14 at 05:34
  • As far as solution 1 goes, RewriteRule ^searchword=(.*) http://www.domain.com/advanced-search#q=$1 [R=301,L,NE], it leads to a 404 error. – manchine Jul 14 '14 at 05:36
  • @manchine yes, sorry. i will leave it in for the reference. do you need help with the first? – asdmin Jul 14 '14 at 05:36
  • @manchine dont use ^ because your pattern does not begin with the query string. searchword=([^&]*) is your expression, if you want to extract the qs for that specific key. – asdmin Jul 14 '14 at 05:37
  • bit lost in the syntax. supposing I leave the conditions as they are, how would the rule look like? Thank you! – manchine Jul 14 '14 at 05:44
  • I made a couple of assupmtions and put up a lot of conditions, but this would be the way to do it the way you want. Please mind the extra condition on the Host: header, I assumed you have several hosts to serve and you want to execute this for only one of them. – asdmin Jul 14 '14 at 05:55
  • Thank you for your kind help. Regrettably, your suggestion leads to a 404 page. – manchine Jul 14 '14 at 06:19
  • play around with http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriteloglevel and share the results, then. Before you do, where the 404 is given? At the source of the redirection or the destination? Are you sure you want to use # to reparate query string? Usually ? is used to do that. – asdmin Jul 14 '14 at 06:21
  • Here's the verbose I have collected after adding RewriteLogLevel 5 to the vhost: http://pastebin.com/MLyzzQmA wondering if index.php/ should be appended to ^/site-search\.html$ The full form of the origina URL i want to manipulate looks like http://www.domain.com/index.php/site-search.html?searchword=children's%20television – manchine Jul 15 '14 at 02:54
  • in the rule I came up with, the resulting glitch is somehow related to the # in the destination URL; do you have any idea why and how it can be solved? – manchine Jul 15 '14 at 04:04
0

After tweaking the patten to match against and adding a ? to the target URL, the syntax is finally working as intended.

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

The ? in the target URL prevents the original query string from being restated. If you are running Apache 2.4, the same result can be achieved by passing the QSD flag to the rewire rule.

Also, if the target URL contains a # (fragment identifier), make sure to place ? before it

manchine
  • 51
  • 1
  • 7