Rewrite path to $_GET - nginx

I want to rewrite this: domain.com/foobar to domain.com?p=foobar.
I don't need subdirectories or multiple variables for this.
I am using nginx, and most other answers where either not what I wanted, or apache.
How can I achieve this?

I was able to solve it using the following rule in the location / {} block:
rewrite ^(.*)$ /index.php?p=$1 last;

Related

nginx rewrite version assets

Help me please. I use nginx and my problem with rewrite.
I have in html code this link http://example.com/admin/assets/123/css/main.css
123 - it`s version of assets
How i can rewrite this path? to http://example.com/admin/assets/css/main.css
I try rule but not work
location ~ /assets/(.*)$ {
rewrite "/[0-9]{3}" /assets/$1 break;
}
You need to capture both parts of the URI, before and after the sequence you wish to delete.
rewrite "^(.*/assets/)[0-9]{3}/(.*)$" $1$2 last;
It is not necessary to place it inside a regular expression location block.

Nginx rewrite rule: Add subfolder to URI if not there

Hello I'm trying to write a Nginx rewrite rule that adds a subdirectory to my URL if it's missing. For example, I need http://example.com to be re-written (and redirected) to http://example.com/legacy-app. Can't seem to find the proper example to do this.
You can use a rewrite directive, but an exact match location block is most efficient:
location = / {
return 301 /legacy-app;
}
See this document for more.
We ended up using this:
rewrite ^/$ /legacy-app/ last;

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.
as an option
location ~ ^/hosts/(.*)$ {
return 301 $scheme://$host/user/$1;
}
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.

simplify a rewrite rule on nginx

I would like to simplify those rewrite rules, don't know if this is possible, here is what I did :
rewrite ^/en/m/(.*)/$ /index.php?lang=en&cat=$1&platform=mobile last;
rewrite ^/en/(.*)/$ /index.php?lang=en&cat=$1 last;
rewrite ^/m/(.*)/$ /index.php?cat=$1&platform=mobile last;
rewrite ^/(.*)/$ /index.php?cat=$1 last;
it works but the number of rewrite rules is quite big..
The parameter /m/ (for mobile) is optional, is there a way to simplify that ? Any idea ?
I finally pass a single parameter and I parse it using php, it works like a charm, thank you Alexey!:)

Nginx Rewrite Not rewriting the url

I have tried this for hours and can't seem to get it. I copied other examples and still can't get rewrite to work.
I need my url on nginx to look like http://myurl.com/main/login, http://myurl.com/somethigelse/home, est. Any Help Appreciated. I'm new to nginx, seems a lot faster.
My nginx rewrite looks like this:
rewrite ^/([^/]+)/([^/]+)$ /index.php?db=$1&action=$2 last;
rewrite ^/([^/]+)/([^/]+)/$ /index.php?db=$1&action=$2 last;
It works for me. Do you tell nginx to reload the configuration (do a sudo service nginx reload)? Otherwise, nginx will still be using the old config.
Note, that you can use one line by making the final slash optional using a question mark:
rewrite ^/([^/]+)/([^/]+)/?$ /index.php?db=$1&action=$2 last;
# ^

Resources