skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    I finally found the a working solution:

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

  2. 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:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search