user2500024
user2500024

Reputation: 3

Redirect specific get variable to other folder. htaccess

days ago I seek the solution in tutorials, manuals, forums, ... Still can not find the solution.

I have the following url:

www.example.com/?unsubscribe=31234242424

I need to redirect to:

www.example.com/newsletter/?unsubscribe=31234242424

Only that variable, thanks.

Sincerely, Alex

Upvotes: 0

Views: 72

Answers (1)

icabod
icabod

Reputation: 7074

You can limit a RewriteRule based on the content of the QUERY_STRING (the bit after the ?). You can do this with a RewriteCond such as the following:

RewriteCond %{QUERY_STRING} unsubscribe=
RewriteRule ^$ /newsletter/ [QSA,R]

The RewriteCond matches any QUERY_STRING that contains "unsubscribe=". Then the RewriteRule will rewrite any empty request to point to "/newsletter/", and append the query string to it. Rewriting only an empty request means that requests in subdirectories will not be redirected.

For example

http://example.com/?unsubscribe=12345 -> http://example.com/newsletter/?unsubscribe=12345
http://example.com/test/?unsubscribe=12345 : no redirect

If you wish to also redirect requests for subdirectories, then replace the rule with the following:

RewriteCond %{QUERY_STRING} unsubscribe=
RewriteRule .* /newsletter/ [QSA,R]

Hope all that makes sense.

Upvotes: 1

Related Questions