Marko I.
Marko I.

Reputation: 562

How to convert this apache rewrite rule to nginx

I've been struggling to convert my past apache rewrite rule to nginx (also not sure if I'm placing it in the right place so would appreciate if you can tell me where to place it).

Basically this was apache rewrite rule on my .htaccess file on Wordpress:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteRule ^/?hosts/(.*)$ /user/$1 [R,L]
</IfModule>

As you can see, I'm using this rule in order redirect from example.com/hosts/username to example.com/user/username

I've used this converting tool https://labs.gidix.de/nginx/ and it outputs this conversion: rewrite ^/?hosts/(.*)$ /user/$1 last; - however I tried placing this in Ajenti's(control panel) advanced custom configuration but it's not working.

Upvotes: 0

Views: 209

Answers (2)

Deadooshka
Deadooshka

Reputation: 478

as an option

location ~ ^/hosts/(.*)$ {
    return 301 $scheme://$host/user/$1;
}

Upvotes: 1

Richard Smith
Richard Smith

Reputation: 49692

You probably need to make this an external redirect so that WordPress will take notice. The cleanest solution would be to place it into a location of its own:

location ^~ /hosts/ {
    rewrite ^/hosts(.*)$ /user$1 permanent;
}

See this document for more.

Upvotes: 0

Related Questions