wazzaday
wazzaday

Reputation: 9664

.htaccess to match anything containing a character

I am trying to write a rule to skip over a fake directory with .htaccess. I have achieved this like

RewriteRule ^fake/(.+)$ /$1 [N,NC]

so now /fake/real would just get /real

But I have a need to target anything which is in the format xx-xx like en-gb or en-us

I have tried something like

RewriteRule ^a{5}/(.+)$ /$1 [N,NC]

which should try and match anything of length 5. But this does not work, and is only half the job anyway. How can I match anything like xx-xx ?

Upvotes: 2

Views: 73

Answers (2)

Jan
Jan

Reputation: 43169

As pointed out in the comments, use:

[a-z]{2}-[a-z]{2}

Upvotes: 2

hjpotter92
hjpotter92

Reputation: 80639

When you do a{5}, it means match the character a five times.

Regular expression visualization

You want to match 2 alphabets, followed by a - and again 2 alphabets:

[a-z]{2}\-[a-z]{2}

The [NC] flag will take care of EN-gb or eN-gB etc. too.

Upvotes: 1

Related Questions