简体   繁体   中英

Mod_rewrite redirect when URL parameters don't match

I need to redirect the page to homepage when its query string parameters don't match.

For example:

www.example.com/something?param1=1111&param2=2222&param3=3333&param4=4444 --> 
--> www.example.com

because param1 and param4 don't match.

I have written a little here:

RewriteCond %{QUERY_STRING} param1=1111
RewriteCond %{QUERY_STRING} !param4=1111
RewriteRule ^/(.*)$ http://www.example.com [R]

but I'm sure that it isn't wokring properly because there are some cases when param4 is not present in the url at all and in that case it would go through the rewrite condition. Could anyone please help me to solve that issue as I'm very new to these configurations. And also, could someone explain to me which flag is better for this situation [R] or [R=301] and what's the difference between them? Thanks

Little bit tricky to do this in mod_rewrite . You can use this back-reference based rule for that:

RewriteEngine On

RewriteCond %{QUERY_STRING}     (?:^|&)param1=([^&]*)
RewriteCond %1::%{QUERY_STRING} (.*?)::(?:|.*&)param4=([^&]*)
RewriteCond %1::%2 !^(.*?)::\1$
RewriteRule ^ /? [L,R]

%1 represents parameter value of param1 and %2 represents parameter value of param4 .

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