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
6
  • 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? Mar 28, 2020 at 20:13
  • i want rewrite first url to second url
    – SS Apps
    Mar 29, 2020 at 6: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, 2020 at 9: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. Mar 30, 2020 at 7:45
  • @PiotrP.Karwasz ok so what is the solution what is rewrite rule of above htaccess rule
    – SS Apps
    Mar 30, 2020 at 12:46

1 Answer 1

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.

6
  • not working show me 404 error page
    – SS Apps
    Mar 30, 2020 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. Mar 30, 2020 at 16:50
  • Thanks for help what is # Your fastcgi_pass configuration . how to find it from our centos server
    – SS Apps
    Mar 31, 2020 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 (usually in a separate file) and a fastcgi_pass directive. Mar 31, 2020 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 1, 2020 at 5:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .