htcaccess redirect a page to another


I've recently came across an issue with WordPress search redirection and felt like sharing the solution.

Problem:
You search is going to ?s=searchText&secondParameter=secondvalue
If you enable permalinks, search may start go be redirected over to /search, losing the second parameter.
Using woocommerce requires the second parameter in order to show results from the store.

Some considerations:
- Using RewriteCond regular expressions can be retrieved later by RewriteRule with %1
- Using RewriteRule regular expressions can be retrieved right away with $1
- Output from %1 will be urldecoded. The usage of [B] is refer as a workaround (didn't work for me).
- RewriteRule cannot work with query strings (getting the query string value or setting up a condition requires RewriteCond)

Solution:
#only catch URLs that contain the "s" query string
RewriteCond %{QUERY_STRING} ^s=([^\&]*)&post_type=product$

#for the URL catched, redirect to /search/ plus the query string value from above, Not Encoded (NE), Last Rule matched (L) and do a 301 redirect to that link [R=103]
RewriteRule ^(.*)$ /search/%1?post_type=product [NE,L,R=301]

Comments

Popular posts from this blog

Mobile development opportunities

Breaking down document locking in SharePoint

Working around X-Frame-Options for iframes