Click here to Skip to main content
15,922,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i tried to convert my htaccess code to nginx conf. I want to redirect files to download.php?file=.... but whatever I do, it doesn't work.

Here is my htaccess code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^/(.*)$ /download.php?file=$1 [R=301]

I've converted it using

and here is the result:
# nginx configuration
location / {
if (-e $request_filename){
rewrite ^/(.*)$ /download.php?file=$1 redirect;

Nginx ignores it, files are located in /home/admin/domains/ and even I used it beside /
but i get the same result.
Is there any solution?

1 solution


The rules for rewrite can be found at:[^]

Looking at your "result" section, it says:

If the file specified exists (in the root of your document directory) and the URL matched the regular expression "^/(.*)$" then rewrite the URL to point to "/download.php?file=$1" and return a "temporary redirect" (302) HTTP code to the client (browser) to ask it to look in the new redirected location.

Without access to the rest of your config or any information about what platform you're running on, it is a bit tricky to tell where to go from here. I am also going to have to make some assumptions:

I'm going to assume:
- That the behavior described above is what you want
- That you have access to your nginx config file (usually located somewhere like /etc/nginx/nginx.conf)
- That ngx_http_rewrite_module is active

What I'd recommend as a start is that you:

* Check that the root directory is set correctly for your site.
It is specified in the nginx.conf file (or your site's config file) and according to what you said above should look something like:
root /home/admin/domains/;

* Check that the file you are trying to access exists in the directory specified as root

* Check the permissions on the file / directory

If these are all correct, let me know and we can take the next steps to solving your problem.

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900