Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apache .htaccess to nginx rewrite rule

I need to change from apache to Nginx but the .htaccess doesn' work on the Nginx server.

I got following in my Apache .htaccess:

RewriteEngine On 

# always run through the indexfile
RewriteRule .$ index.php

# don't let people peek into directories without an index file
Options -Indexes

When I put a .htaccess on the Nginx server, the file gets deleted by the server. In what kind of file do I have to put the .htaccess data (name and suffix please) and how should it be rewritten for Nginx and where do I put the file?

I want all subdirectory files to go through my index.php file so I can include specific files from my index.php file ...

I have tried something like:

# nginx configuration

autoindex off;

location / {
    rewrite .$ /index.php;
}

but it doesn't work. The directory looks something like:

── root
    ├── folder1
    │   └── folder2
    │       └── file.php
    └── index.php

so if I ask for root/folder1/folder2/file.php I want the server to load the root/index.php as I still have the server url request, I can include the root/folder1/folder2/file.php from my index.php

It all works on my apache install, but not on the Nginx server. Please help me.

#

EDIT: Turns out the nginx.conf file has to be above the root folder and is only accessible if you have access to the folders above the root folder. Like server admin access is necessary.

like image 398
Medda86 Avatar asked Mar 18 '14 18:03

Medda86


1 Answers

Nginx doesn't have htaccess files. The code needs to go into the nginx config file. Also, try adding the "last" flag to the rewrite:

# nginx configuration

autoindex off;

location / {
    rewrite .* /index.php last;
}
like image 155
Jon Lin Avatar answered Oct 10 '22 20:10

Jon Lin