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).
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
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