Reputation: 11
I have spent a great many hours trying to find a solution to this and tried many different approaches but nothing I have tried has worked so far.
I would like to redirect a URL with a query string to another URL that contains the value of that query string.
I want to redirect:
https://example.com/component/search/?searchword=XXXXXXXXX&searchwordsugg=&option=com_search
to
https://example.com/advanced-search?search=XXXXXXXXX
Upvotes: 0
Views: 547
Reputation: 45968
You can do something like the following using mod_rewrite at the top of your root .htaccess
file:
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)searchword=([^&]*)
RewriteRule ^component/search/?$ /advanced-search?search=%1 [NE,R=302,L]
The RewriteRule
pattern matches against the URL-path only, which notably excludes the query string. To match against the query string we need a separate condition that checks against the QUERY_STRING
server variable.
%1
is a backreference to the first capturing group in the preceding CondPattern, ie. the value of the searchworld
URL parameter.
The regex (?:^|&)searchword=([^&]*)
matches the searchworld
URL parameter anywhere in the query string, not just at the start (as in your example). This also permits an empty value for the URL parameter.
The NE
flag is required to prevent the captured URL parameter value being doubly encoded in the response. (Since the QUERY_STRING
server variable is not %-decoded.)
The L
flag prevents further processing during this pass of the rewrite engine.
Reference:
Upvotes: 1