Skip to main content

How can I use the converted .htaccess file in the Nginx configuration? [Resolved]

It's about 5 days I'm trying to launch my website without any success. The problem is I use apache on the local and my website works well in local, and my server uses nginx and I cannot use .htaccess conversion inside nginx configuration.

Here is a simplified of my website structure:


See? I have two .htaccess files. One is located on the root and another one is inside files directory. It all works on the localhost, since I use apache on localhost. Now I need to make it working on the server which uses nginx.

I use this website to convert the content of htaccess files to nginx-configuration.

First of all, should I paste the result of conversion in what file? (where is nginx configuration file located? /etc/nginx/nginx.conf ?)

And how can I handle those two .htaccess files? Should I make two nginx files too?

.htaccess file in the root:

RewriteEngine on
Options -Indexes

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([\s\S]*)$ index.php?rt=$1 [L,B,QSA]

ErrorDocument 404 /error404.html

Options -Indexes

    Order Deny,Allow
    Deny from all
    Allow from ::1

    Order Allow,Deny
    Allow from all

.htaccess file which is inside files directory:

   Allow from all

Question Credit: Martin AJ
Question Reference
Asked July 11, 2018
Posted Under: Network
1 Answers

In nginx, all site-specific configurations are included in one server block, and location blocks are used to add different configuration directives for particular URLs.

Overall, the philosophy is quite different from Apache2, so you need to study it in order to understand how to make similar configuration with it.

In your case, nginx configuration directives might look something like this:

location / {
    try_files $uri $uri/ /index.php?rt=$request_uri;

location ~ \.php$ {
    deny all;

location ^~ /index.php {
    # include here the configuration items from nginx default location ~  \.php$ block

location ^~ /files {
    # include here either PHP configuration directives from location ~ \.php$ block if you want PHP scripts executed from here. If you do not want PHP scripts to be executed, then use
    allow all;

These directives are included either in main nginx configuration, or site-specific configuration which exist under /etc/nginx/sites-available directory.

Some explanations on the blocks:

The first location block is the standard front controller pattern implementation on nginx. It means nginx first checks if the required file is found somewhere on server, servers it if one exists. Otherwise it sends the request to index.php, with the original request URI part as an argument to ?rt. This is slightly different than your implementation, since there you use a regex to limit the possible URIs passed as argument.

The second location block rejects access to all URIs ending with .php$.

The third block adds an exception for index.php, which is processed using PHP backend.

The fourth block either sends PHP script requests to PHP backend, or simply allows sending them to the user.

As a disclaimer, I cannot test these rules since I don't know your software environment, so these might not fulfill your requirements, or they might fail for some parts.

credit: Tero Kilkanen
Answered July 11, 2018
Your Answer