phenomenia
phenomenia

Reputation: 109

Htaccess redirect with parameter?

I'm trying to do a .htaccess redirect with a parameter but it's not working. Seems like my regex are also wrong. :(

Original URL 1: http://www.example.com/?team=john-doe 
Original URL 2: http://www.example.com/?team

Target URL: http://www.example.com/company/

I tried:

RewriteEngine on
RewriteCond %{QUERY_STRING} team=john-doe
RewriteRule /company/ [L,R=301]

Any help would be much appreciated.

Upvotes: 2

Views: 2213

Answers (2)

phenomenia
phenomenia

Reputation: 109

Found a generator that works perfectly: https://donatstudios.com/RewriteRule_Generator

# 301 --- http://www.example.com/?team=john-doe => http://www.example.com/company/
RewriteCond %{QUERY_STRING} (^|&)team\=john\-doe($|&)
RewriteRule ^$ /company/? [L,R=301]

Upvotes: 2

MrWhite
MrWhite

Reputation: 46012

Your RewriteRule is malformed, you are missing a pattern (first argument). Try something like:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^team(?:=john-doe)?
RewriteRule ^$ /company/? [R=301,L]

The RewriteRule pattern ^$ matches requests for the domain root. The ? on the end of the RewriteRule substitution strips the query string from the redirected URL (on Apache 2.4+ you can use the QSD flag instead).

The CondPattern ^team(?:=john-doe)? matches either "team=john-doe" (URL#1) or "team" (URL#2) at the start of the query string. The (?: part just makes it non-capturing.

You will need to clear your browser cache before testing.

Upvotes: 0

Related Questions