Matching regex pattern in .htaccess RewriteRule

I am using the following code in .htaccess to change the language of my website:

RewriteRule ^en/?(.*) $1?locale=en_GB [QSA,DPI,L]

So to the visitor the page appears as but in actual fact the address is

The regex I am using is not quite right, however, because I get a 404 error when attempting to access (or, or any other language for that matter).

Even after reading info at and Wikipedia, and using a variety of JavaScript regex helpers, I cannot work out how to get it to match en, en/ or en/dir/pagename.php whilst ignoring any words beginning with en such as enquiry or enquiry-send.php.

What regular expression should I be using so that any of the following addresses work correctly (these are a few examples - page names may contain hyphens - or underscores _):

Thank you.


I have it working using the following expression:

RewriteRule ^en(/?$|/(.*)) $1?locale=en_GB [QSA,DPI,L]


RewriteEngine On
RewriteBase /
RewriteRule ^en/?$ index.php?locale=en_GB [L,QSA]
RewriteRule ^en/([^?]*) $1?locale=en_GB [L,QSA]

That will match all of your test URLs, and send en alone to the index.php file

Need Your Help

Is it possible to get values from query string with same name?

python query-string tornado

I want to know if it's possible to get values from this query string?

How to override an inherited class property in Delphi?

delphi oop properties

In one file, I have a baseclass with a ID-property:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.