Nginx rewrites the configuration for CodeIgniter applications in the main subdirectory of the CodeIgniter application
Moving from Apache to a new Nginx server I can't figure out this new rewrite code ...
I have a site that has a main CodeIgniter application, say www.codeigniterapp.com .
Then I have separate CodeIgniter apps like www.codeigniterapp.com/random-directory/app2 etc.
The main CodeIgniter app works fine, but the apps in the subdirectory won't fire when loading anything other than the base url.
For example, www.codeigniterapp.com/random-directory/app2 works, but www.codeigniterapp.com/random-directory/app2/some-action-here will print a 404 error. I need some kind of action to send to index. php. It works like this: www.codeigniterapp.com/random-directory/app2/index.php?/some-action-here
I need help generating nginx rewrite code. The old apache.htaccess in app2 -directory was as follows:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /random-directory/app2/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
Now I am using Nginx enabled vhost configuration (Code Igniter):
server {
listen 80;
server_name codeigniterapp.com www.codeigniterapp.com;
root /var/www/codeigniterapp.com;
add_header Strict-Transport-Security max-age=15768000;
add_header X-UA-Compatible "IE=Edge,chrome=1";
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "NOSNIFF";
autoindex off;
index maintenance.html index.php index.html index.htm;
# Hide files that begin with a "."
location ~ /\. {
access_log off;
log_not_found off;
return 404;
}
# Images and text files are served normally, while everything else is downloaded.
# PHP will not execute!
location /uploads {
types {
image/gif gif;
image/jpeg jpeg jpg;
image/png png;
text/plain txt;
application/pdf pdf;
}
default_type application/octet-stream;
location ~ \.php$
{
break;
}
}
# Codeigniter; rewrite default controller and "/index.php" back to root domain.
if ($request_uri ~* ^(/welcome(/index)?|/index(.php)?)/?$) {
rewrite ^(.*)$ / permanent;
}
# Codeigniter; rewrite "controller/index" back to root "controller".
if ($request_uri ~* index/?$) {
rewrite ^/(.*)/index/?$ /$1 permanent;
}
# Codeigniter; remove trailing slashes.
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
# Main location block. If the file or folder exists, display it, otherwise fallback
# to the site location block.
location / {
try_files $uri $uri/ @site;
}
# Codeigniter; pass the current request to the index.php file for Codeigniter to handle.
location @site {
rewrite ^/(.*)$ /index.php?/$1 last;
}
# Proxy PHP files to PHP-FPM.
# If the file doesn't exist then throw a 404 (defined below).
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param CI_ENV production;
include fastcgi_params;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
}
# Pass not-found errors back to Codeigniter to handle.
error_page 404 /index.php;
# Cache static files. Don't bother logging them.
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
access_log off;
log_not_found off;
expires 30d;
}
}
source to share
Problem solved! I added the following to my vhost config
location /random-directory/app2/ {
if ($request_uri ~ "^system.*"){
rewrite ^/random-directory/app2/(.*)$ /random-directory/app2/index.php?/$1 last;
}
if (!-e $request_filename){
rewrite ^/random-directory/app2/(.*)$ /random-directory/app2/index.php?/$1 last;
}
}
source to share