1

I've already seen Regex not equal to string and Regular expression for a string that does not start with a sequence but I can't find the right solution to my regular expression.

I need to do this: "if my HOST doesn't start with eiter s, m or www like (s.example.whatever, m.example.whatever or www.example.whatever) then redirect it to www.example.whatever.

Like this:

RewriteCond %{HTTP_HOST} ((.+)\.)+example\.(.+)$
RewriteCond %2 ^(?!s|m|www$)
RewriteRule (.*) http://www.example.com$1 [R=301,QSA,L]

It doesn't work after many tries at https://regex101.com/ What am I missing?

Community
  • 1
  • 1
Olivier Pons
  • 15,363
  • 26
  • 117
  • 213

1 Answers1

1

You can use negation in RewriteCond:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^(s|m|www)\. [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,NE,L]
anubhava
  • 761,203
  • 64
  • 569
  • 643