0

Anyone tell me how to rewrite URL in Nginx

https://dl.expamledomain.com/download.php?p=524&st=mmmnGreUwmpRTGl7RO9NtGLhgQZrQ1IW&fn=file.zip

to

https://dl.expamledomain.com/dwn/524/mmmnGreUwmpRTGl7RO9NtGLhgQZrQ1IW/file.zip

second our download.php file in the root public_html folder.

I have used this rule on .htaccess

RewriteEngine On

RewriteRule ^dl/(\w+)/(\w+)/(.*)/?$ download.php?pt=$1&tk=$2&flen=$3 [L,QSA,NC]

its working fine but I try many on Nginx rule like this

I try this code in Nginx but not working

    http{

       server {
          listen       80;
          server_name  *.oursitename.com;

   location / {
    rewrite ^(?i)/dl/(\w+)/(\w+)/(.*)/?$ /download.php?pt=$1&tk=$2&flen=$3 last;
 }

  location = /download.php {
    # Your fastcgi_pass configuration
}
    }

its show me this error

" open() "/home/username/public_html/dl/515/25xfkUNYOGvvOt0z7ko9YkpHCWZrlRfX/file72.exe" failed (2: No such file or directory),"

our file location like this

1.Home
    1.Username
     1.public_html
       1.downloadfolder
       2.download.php
       3.index.php
       4.htaccess
SS Apps
  • 1
  • 1
  • If the client asks for the first one, you want to send a `HTTP` redirect to the second one? Or rather you want to rewrite the second one into the first one? – Piotr P. Karwasz Mar 28 '20 at 20:13
  • i want rewrite first url to second url – SS Apps Mar 29 '20 at 06:16
  • 1
    This is not very good approach, please see my comment for the answer by @PiotrP.Karwasz. This kind of things need sanitisation, validation steps, to verify the request for specific characters. usually in a real programming language and not conf files at the server level. – JonnieJS Mar 29 '20 at 09:21
  • The example Apache configuration is inconsistent with your question:1. it rewrites the second URL into the first one, 2. it rewrites the URI Path `/dl/...` not `/dwn/...` as in your example. I deleted my answer to which @JonnieJS is referring, since it is inconsistent with what you are really asking. – Piotr P. Karwasz Mar 30 '20 at 07:45
  • @PiotrP.Karwasz ok so what is the solution what is rewrite rule of above htaccess rule – SS Apps Mar 30 '20 at 12:46
  • The error you are getting, means that control does not reach the `location /` block. If you have multiple `server` blocks, it might reach another virtual host (according to the URL and `server_name`). Increasing the log level to debug (cf. [error_log directive](https://nginx.org/en/docs/ngx_core_module.html#error_log)) allows you to pick into the `location` selection, while `rewrite_log on;` logs the rewritten URLs at a `notice` level. – Piotr P. Karwasz Apr 01 '20 at 19:53

1 Answers1

0

NGINX rewrite rules always use absolute URI Paths, so in order to rewrite your second URL into the first one (which is what the provided Apache2 rule does), you just need to add a couple of /:

location / {
    rewrite ^(?i)/dl/(\w+)/(\w+)/(.*)/?$ /download.php?pt=$1&tk=$2&flen=$3 last;
}
location = /download.php {
    # Your fastcgi_pass configuration
}

The NC flag is translated into the PCRE mode modifier (?i) (cf. this question), while the QSA flag is the default NGINX behaviour.

Remark: Under Apache2 it is also better to put your rewrite rules in the server config, instead of .htaccess files and disable them altogether: you gain in performance and they will not be published on the Web if you make a configuration mistake.

Piotr P. Karwasz
  • 5,748
  • 2
  • 11
  • 21
  • not working show me 404 error page – SS Apps Mar 30 '20 at 16:21
  • A `/` was missing in `location = /download.php`. Of course you can use a more general location for all PHP files: `location ~ \.php$`, which you probably already have. The NGINX's error log (`/var/log/nginx/error.log`) will tell you whether the PHP file was executed or not. – Piotr P. Karwasz Mar 30 '20 at 16:50
  • Thanks for help what is # Your fastcgi_pass configuration . how to find it from our centos server – SS Apps Mar 31 '20 at 12:54
  • It is a comment line, it stands for all the directives needed to pass the request to the **PHP-FPM** server, i.e. a set of params [like this](https://www.nginx.com/resources/wiki/start/topics/examples/phpfcgi/) (usually in a separate file) and a [fastcgi_pass](https://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_pass) directive. – Piotr P. Karwasz Mar 31 '20 at 17:21
  • show me error in error file " open() "/home/username/public_html/dl/515/25xfkUNYOGvvOt0z7ko9YkpHCWZrlRfX/file72.exe" failed (2: No such file or directory)," – SS Apps Apr 01 '20 at 05:38
  • I updated our main post please check it we face the error . and our main post I also show our file hierarchy – SS Apps Apr 01 '20 at 05:57