Saif Bechan
Saif Bechan

Reputation: 17121

Checking if language is set in url with regex

I am working on a multi language file. My urls look something like this:

http://www.example.com/en/about/info
http://www.example.com/nl/about/info

Now I use a small regex script that redirect the user when they use a link without language. The script looks like this:

preg_match('~^/[a-z]{2}/~', $_SERVER['REQUEST_URI'])

This finds out whether there is a language set en|nl|de etc. This works fine on all links except for these:

http://www.example.com/en
http://www.example.com/nl

There is no trailing slash so the regex can not find the given values.

Anyone know a fix for this?

Upvotes: 1

Views: 1961

Answers (2)

serg
serg

Reputation: 111285

preg_match('~^/[a-z]{2}(?:/|$)~', $_SERVER['REQUEST_URI']

Upvotes: 3

erenon
erenon

Reputation: 19118

preg_match('~^/[a-z]{2}(?:/)?~', $_SERVER['REQUEST_URI'])

Upvotes: 1

Related Questions