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:
/mywebsite
/application
/files
file1.php
.htaccess
/public
/css
/js
.htaccess
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
<Files *.php>
Order Deny,Allow
Deny from all
Allow from ::1
</Files>
<Files index.php>
Order Allow,Deny
Allow from all
</Files>
.htaccess
file which is inside files
directory:
<Files *.php>
Allow from all
</Files>
Best Answer
In nginx, all site-specific configurations are included in one
server
block, andlocation
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:
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 toindex.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.