Nginx rewrite - not working - nginx

I have simple nginx rewrite I can't get to work.
I have this url:
https://example.com/accessories/3427-tote-bag-grey-212345050033.html
I want to redirect to:
https://example.com/dk/accessories/3427-tote-bag-grey-212345050033.html
My nginx config:
location / {
index /index.php;
rewrite ^/dk/$1/$2.html /$1/$2.html last;
}
any idea want is wrong here?

$1 and $2 are used for captures. You have to use pattern to with groups to create captures
Try below
location / {
index /index.php;
rewrite ^/([^/]+)/([^/]+).html$ /dk/$1/$2.html last;
}
Or you can do something like below
location / {
index /index.php;
location /accessories/ {
alias <yourroot>/dk/accessories/;
}
}

Related

How to edit url without updating in browser - nginx config

I want the Request URL to be converted from
http://host.com/newname/abc?def= to
http://newname/abc?def=
here is the config file
server {
listen 80;
server_name
host.com
location / {
rewrite ^(.+)/$ $1 permanent;
rewrite ^(.+)/index\.html$ $1 permanent;
rewrite ^(.+)\.html$ $1 permanent;
try_files /$host/public/$uri #webserver;
}
}
Adding above line worked form me
location / {
rewrite ^ $scheme://$request_uri? permanent;
}
But it replaces the url in user browser which i donn't want to happen.
Any way to achieve it
You can try this:
location /newname{
proxy_pass http://example.com/newname/abc?def=;
}
or
location /newname{
proxy_pass http://newname/abc?def=;
}

How to rewrite randomly generated nginx path by taking the first segment?

I have a prestashop instance that generates a random URL for admin access. The only rule is that the path starts with "admin".
The following rule works just fine, but its hardcoded by hand:
location /admin6908ewwh6/ {
if (!-e $request_filename) {
rewrite ^/.*$ /admin6908ewwh6/index.php last;
}
}
I tried to rewrite it into this:
location ^(/admin.*?)(\w+)/ {
if (!-e $request_filename) {
rewrite ^/.*$ $1/index.php last;
}
}
But this is not working, and I don't know why since according to this regex matcher ( https://www.regextester.com/102896 ) when I put a ^(/admin.*?)(\w+) regex against a test string /admin6908ewwh6/index.php/sell/catalog/products/new?_token=_JC1fQPwgvwnhZTWyeGVTy4nET350GC4Aro888TuzDA& it just grabs what I need to take.
Can somebody explain me why these two location blocks are not equivalent?
The problem is $1. Numeric captures are assigned by the last regular expression to be evaluated, which in this case is the rewrite statement (despite there being no parentheses in the regular expression).
One solution is to make the capture in the rewrite statement, for example:
location /admin {
if (!-e $request_filename) {
rewrite ^(/admin[^/]+)/ $1/index.php last;
}
}
Or without the if block:
location /admin {
try_files $uri $uri/ #admin;
}
location #admin {
rewrite ^(/admin[^/]+)/ $1/index.php last;
}
Or without the rewrite statement:
location ~ ^(/admin[^/]+)/ {
try_files $uri $uri/ $1/index.php$is_args$args;
}
Ensure that this last location block is below the block that processes .php URIs.

How to convert url segment to querystring with Nginx rewrite

I whould like this simple rewrite rule:
http://example.com/8743b52063cd84097a65d1633f5c74f5?param1=999&param2=2222
to be redirected to:
http://example.com/index.php?param1=999&param2=2222&hash=8743b52063cd84097a65d1633f5c74f5
The following is my default location:
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
How can I achieve this using Nginx rewrite?
Using a rewrite statement:
rewrite "^/(\w{32})$" /index.php?hash=$1 last;
Or, within a location block:
location ~ "^/(?<hash>\w{32})$" {
rewrite ^ /index.php?hash=$hash last;
}

Nginx rewrites static files to index.html

I'm trying to work on a single page app - I need to rewrite all urls to index.html but allow existing static files (.css and .js) to be served as they normally would be in a browser.
This is the code that I'm trying to use to re-write but it serves my static files to the re-write as well
if (!-e $request_filename)
{
rewrite ^/(.*)$ /?/$1 last;
break;
}
you don't actually need a rewrite for that in nginx, just use try_files like so:
location / {
try_files $uri /index.html;
}
what this does is for all url's:
try the exact static filename match, and serve it if present
if 1 didn't serve anything, then server /index.html instead
see http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
This should work:
server {
listen 1.2.3.4:80;
server_name domain.eu;
root /usr/local/www/domain.eu/public;
try_files $uri #rewrites;
location #rewrites {
rewrite ^/favicon.ico$ /pictures/favicon.ico last;
rewrite ^ /index.html last;
}
}

nginx : rewrite rule to remove /index.html from the $request_uri

I've seen a few ways to rewrite the $request_uri and add the index.html to it when that particular file exists in the file system, like so:
if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}
but i was wondering if the opposite is achievable:
i.e. when somebody requests http://example.com/index.html, they're redirected to http://example.com
Because the nginx regexp is perl compatible, i tried something like this:
if ( $request_uri ~* "index\.html$" ) {
set $new_uri $request_uri ~* s/index\.html//
rewrite $1 permanent;
}
but it was mostly a guesswork, is there any good documentation describing the modrewrite for nginx ?
I use the following rewrite in the top level server clause:
rewrite ^(.*)/index\.html$ $1 permanent;
Using this alone works for most URLs, like http://example.com/bar/index.html, but it breaks http://example.com/index.html. To resolve this, I have the following additional rule:
location = /index.html {
rewrite ^ / permanent;
try_files /index.html =404;
}
The =404 part returns a 404 error when the file is not found.
I have no idea why the first rewrite alone isn't sufficient.
The following config allowed me to redirect /index.html to / and /subdir/index.html to /subdir/:
# Strip "index.html" (for canonicalization)
if ( $request_uri ~ "/index.html" ) {
rewrite ^(.*)/ $1/ permanent;
}
For some reason most of the solutions mentioned here did not work. The ones that worked gave me errors with missing / in the url. This solution works for me.
Paste in your location directive.
if ( $request_uri ~ "/index.html" ) {
rewrite ^/(.*)/ /$1 permanent;
}
This one works:
# redirect dumb search engines
location /index.html {
if ($request_uri = /index.html) {
rewrite ^ $scheme://$host? permanent;
}
}
For the root /index.html, the answer from Nicolas resulted in a redirect loop, so I had to search for other answers.
This question was asked on the nginx forums and the answer there worked better.
http://forum.nginx.org/read.php?2,217899,217915
Use either
location = / {
try_files /index.html =404;
}
location = /index.html {
internal;
error_page 404 =301 $scheme://domain.com/;
}
or
location = / {
index index.html;
}
location = /index.html {
internal;
error_page 404 =301 $scheme://domain.com/;
}
This is working for me:
rewrite ^(|/(.*))/index\.html$ /$2 permanent;
It covers both the root instance /index.html and lower instances /bar/index.html
The first part of the regex basically translates as: [nothing] or /[something] - in the first case $2 is an empty string so you redirect to just /, in the second case $2 is [something] so you redirect to /[something]
I actually went a bit fancier to cover index.html, index.htm, and index.php
rewrite ^(|/(.*))/index\.(html?|php)$ /$2 permanent;
The solutions quoting $scheme://domain.com/ assume that the domain is hard-coded. It was not in my case and so I used:
location / {
...
rewrite index.html $scheme://$http_host/ redirect;
... }

Resources