简体   繁体   中英

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]
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

it's works

Options +FollowSymLinks
RewriteEngine On
RewriteBase /test/   
RewriteRule ^en/(.*) $1?lang=en_GB [L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM