简体   繁体   中英

Redirect the URL from one query string to another

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

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 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