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 mysite.com/en/index.php but in actual fact the address is mysite.com/index.php?locale=en_GB.

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

Even after reading info at regular-expressions.info 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 _):

mysite.com/en
mysite.com/en/
mysite.com/en/index.php
mysite.com/en/enquiry.php
mysite.com/en/enquiry-send.php

Thank you.

EDIT

I have it working using the following expression:

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

Answers


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.