Score:0

Nginx permanent redirects from subfolder

mk flag

What im trying to achieve is redirects from mysite.com/en/something/something1 to mysite.com/something/something1 is there any way to remove static subpath /en/ from the url using nginx conf?

Score:1
ke flag

You can use Nginx's rewrite directive to remove the /en subpath from your URL. Here's an example:

server {
    listen 80;
    server_name mysite.com;

    # Redirect /en/ URLs to their equivalent URL without /en/
    rewrite ^/en/(.*)$ /$1 permanent;

    # Your other server configuration goes here
    ...
}

The rewrite directive matches any URLs that start with /en/ and captures everything after that subpath using the regular expression (.*). It then rewrites the URL to remove the /`en/ subpath and redirects the client to the new URL using the permanent flag, which tells the client to cache the redirect permanently.

Note that this configuration assumes that you want to permanently redirect all URLs that start with /en/. If you only want to remove the /en/ subpath for a specific set of URLs, you'll need to adjust the regular expression in the rewrite directive to match only those URLs.

I hope that helps!

Score:0
us flag

Another alternative to above:

server {
    location ~ ^/en(/.+) {
        return 301 $1;
    }

    location / {
        ... other configuration
    }
}
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.