David
David

Reputation: 3358

.htaccess redirect only if GET parameter exist

I have the following line in my .htaccess file:

Redirect 301 /folder1 https://www.example.com/folder2/file.php

This will redirect everything from /folder1 to https://www.example.com/folder2/file.php.

I need a condition to only allow this redirection if the URL contains a mykey= GET parameter, else ignore this redirection command.

How can I do that?

Upvotes: 1

Views: 382

Answers (2)

anubhava
anubhava

Reputation: 786289

You cannot do this using Redirect directive that does basic URI matching.

You will need to use mod_rewrite based rules for this like this:

RewriteEngine On

RewriteCond %{QUERY_STRING} (^|&)mykey= [NC]
RewriteRule ^folder1(/|$) /folder2/file.php [R=301,L,NC]

Make sure to clear your cache before testing.

References:

Upvotes: 1

David
David

Reputation: 3358

I finally found the a working solution:

RewriteCond %{REQUEST_URI} ^/folder1/
RewriteCond %{QUERY_STRING} mykey=
RewriteRule ^folder1\/$ /folder2/file.php$1 [R=301,L]

Upvotes: 0

Related Questions