HTTP Language and worker redirect

How can I use worker to redirect based on browser language

something like this

RewriteCond %{HTTP:Accept-Language} ^ar [NC]
RewriteRule ^$ /ar/ [L,R=301]

request.headers.get('accept-language') will return the header in question and Response.redirect() will allow you to send a redirect.

The rest is custom JavaScript.

1 Like

Many thanks for your quick post. This is my worker script at the moment. How can I adapt it ?

I want anything at to redirect e.g. if browser is arabic and visits it should rediirect to

I have some exclusions as you can see

addEventListener(‘fetch’, event => {


  • Respond to the request
  • @param {Request} request

async function handleRequest(request) {

//define url_path as the url path
var url_path = new URL(request.url).pathname

//log the request path in concole
// console.log('The url: ', url_path)

// if statement, if request url does not have /path/ redirect to /path + the original uri

if (!url_path.startsWith(’/en’) && !url_path.startsWith(’/robots.txt’) && !url_path.startsWith(’/sitemap.xsl’) && !url_path.startsWith(’/browserconfig.xml’) && !url_path.startsWith(’/manifest.json’) && (!url_path.startsWith(’/js’) && (!url_path.startsWith(’/images’)))))
return new Response("",{
status: 302,
headers: {
‘Location’: “/en” + url_path



//otherwise return regular response
else {
return await fetch(request)


I am afraid code related questions are a tad off-topic for here and better asked over at StackOverflow for example.

But you essentially add an additional if to your code where you evaluate the header accordingly and send a redirect if desired.

OK thanks!

And you don’t necessarily need a new response object. The function mentioned earlier will do the trick too.

Can you reach out to me to see if you can help work on this for us, happy to pay for your time.

Have you tried over at StackOverflow or a similar site? There you should find the right direction relatively quickly.

The forum here is unfortunately not exactly the best place for such paid gigs, e.g. Upwork would be more suitable.

But generally, why don’t you want to use the Apache configuration? That you already have and it would not be a potentially paid feature.

Web servers are IIS and redirect seems to be working well using workers at the moment we just need to add further logic to it to redirect based on the users browser language. for purely English and Arabic only.

I am sure IIS has a similar redirect function. But if you really want to do it with Workers aforementioned line should do the trick. Have you tried it yourself or asked at StackOverflow?

Tried to modify ourselves, will try stackoverflow for further help otherwise, thanks