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)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.