PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, October 16, 2022

[FIXED] How to add language code after domain name on nginx

 October 16, 2022     magento2.2, nginx, url-rewriting     No comments   

Issue

I would like to add en-us language code on URL. The expectation is as follows.

When a user hits on http://example.com/, page should redirect users to http://example.com/en-us/ Note: I have achieved this with the following code.

location = / {
      rewrite ^ /en-us/ redirect;
  }

How to redirect customers to following scenarios

  • http://example.com/contact, should redirect to http://example.com/en-us/contact
  • http://example.com/en-us/, it shouldn't append extra en-us in url.
  • http://example.com/en-in/, it shouldn't append en-us in url.

Simply, what i want to achieve is if there is no en-us in url, then we should add en-us in XX-XX place. http://example.com/XX-XX/contact


Solution

# Do nothing for assets (paths with a dot)

location ~ \. {
}

You can match locations that don't start with the required path by using a negative lookahead regex (?!)

# Paths that don't start  with /en-us/ are redirected:

location ~ ^(?!/en-(us|in)/) {
    rewrite ^/(.*)$ /en-us/$1 redirect;
}

Or using an if block:

if ($request_uri !~ "^/en-(us|in)/")
{
    return 301 /en-us/$request_uri;
}


Answered By - jspcal
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing