I have 3 folders under the document root of the website:
/usr/share/nginx/example.com/public_html# ls -la
drwxr-xr-x 5 root root 73 Jul 17 07:28 app
drwxr-xr-x 4 root root 76 Jul 17 07:28 base
-rw-r--r-- 1 root root 211 Jul 8 14:32 .htaccess
drwxr-xr-x 4 root root 57 Jul 22 11:10 public
Content of .htaccess:
/usr/share/nginx/example.com/public_html# cat .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) public/$1 [L]
</IfModule>
Content of 'public' subdirectory:
/usr/share/nginx/example.com/public_html/public# ls -la
drwxr-xr-x 2 root root 27 Jul 17 07:28 css
-rw-r--r-- 1 root root 219 Jun 23 10:17 .htaccess
-rw-r--r-- 1 root root 6860 Jul 17 07:15 index.php
drwxr-xr-x 2 root root 26 Jul 17 07:15 js
And finally content of underlying .htaccess:
/usr/share/nginx/example.com/public_html/public# cat .htaccess
AddDefaultCharset UTF-8
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>
As this is based on YII framework, I took their Nginx sample configuration and modified it this way:
server {
access_log /usr/share/nginx/example.com/logs/access.log main;
error_log /usr/share/nginx/example.com/logs/error.log;
rewrite_log on;
server_name example.com www.example.com;
root /usr/share/nginx/example.com/public_html;
charset utf-8;
index index.php index.html;
location / {
rewrite ^(.*)$ /public$1 last;
}
location /public {
try_files $uri $uri/ /public/index.php?_url=/$uri;
}
location ~ ^/(protected|framework|themes/\w+/views) {
deny all;
}
#avoid processing of calls to unexisting static files by yii
location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
try_files $uri =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php {
fastcgi_split_path_info ^(.+\.php)(.*)$;
#let yii catch the calls to unexising PHP files
set $fsn /index.php;
if (-f $document_root$fastcgi_script_name){
set $fsn $fastcgi_script_name;
}
fastcgi_pass unix://var/run/php-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fsn;
#PATH_INFO and PATH_TRANSLATED can be omitted, but RFC 3875 specifies them for CGI
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fsn;
}
# prevent nginx from serving dotfiles (.htaccess, .svn, .git, etc.)
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
}
This behaves the way that whatever page I access on the website, every-time only index page is shown.
Any help appreciated. Thanks.
Edit 1
To make the question clearer, what I need to achieve is to rewrite htaccess files above so it works properly in Nginx.
This part is what I came up with:
location / {
rewrite ^(.*)$ /public$1 last;
}
location /public {
try_files $uri $uri/ /public/index.php?_url=/$uri;
}
Edit 2 - solution
I replaced this:
location / {
rewrite ^(.*)$ /public$1 last;
}
location /public {
try_files $uri $uri/ /public/index.php?_url=/$uri;
}
by this:
location / {
rewrite ^(.*)$ /public/$1 last;
}
location /public {
rewrite ^/public/(.*)$ /public/index.php?_url=$1;
}
I've added a slash sign in the first rewrite and used rewrite instead of try_files in case of second rule.