Steve
Steve

Reputation: 1423

mod_rewrite this - multiple conditions or can I just pass all vars over together?

Due to a reverse proxy setup I'm having to pass an extra query var which the proxy can't using mod_rewrite. The proxy is at /search however I'm using /find on all pages as a mod_rewrite to /search to pas the query var s=gsacollection.

See example:

# Direct link to search which passes collection var
# eg http://www.domain.com/find
RewriteRule ^find$ /search?s=gsacollection [NC]

#Rewrite all query vars
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^find(.*)$ /search?%1 [NC,L] 

I'm trying to capture multiple variables for mod_rewrite that are being sent. The issue is I don't always know which ones are being sent over. This is an attempt to blanket capture them. Suggestions?

I want to pass all the query strings after /find? to /search?

EG here are some sample URIs coming in:

find?q=test&sort=date:D:L:d1&num=10&s=gsacollection&l=en&start=10

find?q=tfsa&sort=date:D:L:d1&num=10&s=gsacollection&l=en&filter=0

find?q=tfsa&filter=0&num=10&s=gsacollection&l=en&sort=date%3AD%3AS%3Ad1

If a blanket capture won't work then I will have to look at setting up multiple RewriteCond rules, wondering if there's a way I can combine these in a way I can pass vars from each condition to build the rewrite rule (eg group)?

# Grab everything after /find and replace with /search if these query vars exist
RewriteCond %{QUERY_STRING} q=(.*) [AND]
RewriteCond %{QUERY_STRING} s=(.*)
RewriteRule ^find(.*)$ /search$1 

Upvotes: 1

Views: 1042

Answers (1)

anubhava
anubhava

Reputation: 785128

Try using this code in your .htaccess file under $DOCUMENT_ROOT:

Options +FollowSymLinks -MultiViews
RewriteEngine on

RewriteOptions MaxRedirects=10

RewriteRule ^find/?$ search?s=gsacollection [QSA,L,NC]

Make sure you don't have any other conflicting mod_rewrite rule here. QSA flag will make sure to append all query parameters to merge with s=gsacollection parameter.

Upvotes: 1

Related Questions