CodeIgniter URL rewrite with Nginx - codeigniter

I'm trying to migrate a site under CodeIgniter from Apache to Nginx. My first tests show very good performances, so it worst the try.
But I can't find the correct Nginx configuration to replace those RewriteRules :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I found this recipe on Nginx website https://www.nginx.com/resources/wiki/start/topics/recipes/codeigniter/
location / {
try_files $uri $uri/ /index.php;
}
It works in most cases, except when I have a trailing slash. Meaning
http://example.com/controller/param/
will call http://example.com/controller/function/index.php (and return a 404)
instead of http://example.com/index.php/controller/function/ ...as it usually do with Apache rewrite.
I tried to add a rewrite :
rewrite ^/(.*)/$ /$1 permanent;
But it redirect POST to GET, so I loose all my post data...
Any clue ?
Thanks

Check if this helps. Replace both occurrences of <source_directory>; with your correct value:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root <source_directory>;
rewrite_log on;
index index.html index.php;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
if ($host ~* ^www\.(.*))
{
set $host_without_www $1;
rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
}
# canonicalize codeigniter url end points
# if your default controller is something other than "welcome" you should change the following
if ($request_uri ~* ^(/welcome(/index)?|/index(.php)?)/?$)
{
rewrite ^(.*)$ / permanent;
}
# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
rewrite ^/(.*)/index/?$ /$1 permanent;
}
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/(.+)/$ /$1 permanent;
}
# removes access to "system" folder, also allows a "System.php" controller
if ($request_uri ~* ^/system)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# use fastcgi for all php files
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME <source_directory>$fastcgi_script_name;
include fastcgi_params;
}
# deny access to apache .htaccess files
location ~ /\.htaccess
{
deny all;
}
error_page 404 /404.html;
location = /40x.html
{
}
error_page 500 502 503 504 /50x.html;
location = /50x.html
{
}
}

Related

Add slash and cut extension of file (nginx rules)

I tried to figure out how to add "/" to the end of every url.
And this url need to be without extension.
For example:
example.com/about.php; example.com/about.html => example.com/about/
I'm dummy in configuration nginx rewrite rules.
This is my config:
server{
root /usr/share/nginx/www/example.com;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name example.com www.example.com;
client_max_body_size 20M;
# BEGIN W3TC Browser Cache
gzip on;
gzip_types text/css text/x-component application/x-javascript application/json application/javascript text/javascript text/x-js text/richtext image/svg+xml text/plain text$;
# END W3TC Browser Cache
location /blog {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
# try_files $uri $uri/ /index.php;
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location ~* ^.+\.(rss|atom|jpg|jpeg|gif|png|ico|rtf|js|css|json)$ {
expires max;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location / {
try_files $uri $uri.html $uri/ #extensionless-php;
index index.php index.html index.htm;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
}
I founded some solutions but i can't to combine it, because I'm noob in nginx.
You need to change part of your config like that
location / {
rewrite ^(/.*[^/])\.(html|php)$ $1/ permanent;
rewrite ^(/.*[^/])(?!/)$ $1/ permanent;
try_files $uri $uri.html $uri/ #extensionless-php;
index index.php;
}
location #extensionless-php {
rewrite ^(.*)/$ $1.php last;
}
First line rewrite ^(/.*[^/])\.(html|php)$ $1/ permanent; will redirect all requests with extensions .html or .php
rewrite ^(/.*[^/])(?!/)$ $1/ permanent;
Will add / to requests, which uri end without /

Can't run mod_rewrite with Phalcon application

I use Ubuntu and nginx 1.6.2. I can't run Phalcon application because it uses mod_rewrite rules, but in manual I couldn't find nginx rules, only Apache. Here's my configuration:
server {
listen 80;
server_name invo;
index index.php index.html index.htm;
set $root_path '/home/me/invo/public';
root $root_path;
location / {
try_files $uri $uri/ /index.php;
# These rules doesn't work
rewrite ^/$ /public/ break;
rewrite ^(.*)$ /public/$1 break;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?_url=/$1 break;
}
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
root $root_path;
}
location ~ /\.ht {
deny all;
}
}
I commented three rules that doesn't work for me. All the time I open http: //invo/cat/index http: //invo/something I always see the main page.
Here's the Phalcon rules for Apache:
# /invo/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
# /invo/public/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>
Here's my result working config:
server {
listen 80;
server_name invo;
index index.php index.html index.htm;
set $root_path '/home/me/invo/public';
root $root_path;
try_files $uri $uri/ #rewrite;
location #rewrite {
rewrite ^/(.*)$ /index.php?_url=/$1;
}
location ~ \.php {
fastcgi_index /index.php;
# The difference from Phalcon manual in this line
fastcgi_pass unix:/var/run/php5-fpm.sock;
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
# Comment out this line too
#fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
root $root_path;
}
location ~ /\.ht {
deny all;
}
}

magento nginx redirection for migration

I have 2 website. Let's say website A and B.
Both are using Magento.
Almost all of products in B also built-in in A.
Website B is going to be closed hence all of its matching products will be redirected to website A.
For other page (non-matching products & other url) will be redirected to A's homepage.
I created script to map matching products from B to A (put it under name mapping.txt), it contains around 20000 url.
To do the redirection, I'm using nginx HttpMapModule (http://wiki.nginx.org/HttpMapModule)
I was able to map it by using this conf:
map $uri $new{
default http://www.a.com/?ref=b;
include /some/path/mapping.txt;
}
server {
listen 80 ;
listen 443 ssl;
server_name www.b.com;
root /some/path/www/b;
#redirect b
rewrite ^ $new redirect;
location / {
index index.html index.php;
try_files $uri $uri/ #handler;
expires 30d;
}
.
.
.
}
The problem is, I want to be able to access B's admin page.
Because of the default map, I can not access http://www.b.com/admin anymore.
Then, I changed the conf:
map $uri $new{
default http://www.a.com/?ref=b;
include /some/path/mapping.txt;
}
server {
listen 80 ;
listen 443 ssl;
server_name www.b.com;
root /some/path/www/b;
location / {
#redirect b
rewrite ^ $new redirect;
index index.html index.php;
try_files $uri $uri/ #handler;
expires 30d;
}
.
.
.
}
By this conf, I still unable to access http://www.b.com/admin but I can access http://www.b.com/index.php/admin but the css and js are not loaded because request to (http://www.b.com/skin/..... and http://www.b.com/js/..... are redirected to http://www.a.com/?ref=b)
But this conf is also problematic, it won't redirect to A's homepage if I type http://www.b.com/index.php/customer/account/login
example content of mapping.txt:
/product-abc.html http://www.a.com/product-abc12.html;
/category-a/product-xyz.html http://www.a.com/product-xyz.html;
/category-b/category-d/product-123.html http://www.a.com/category-e/product-12z.html;
You can use a simple domain redirect on .htaccess for these generic cases...
RewriteCond %{HTTP_HOST} ^www.a.com$ [NC]
RewriteRule ^(.*)$ http://www.b.com/$1 [R=301,L]

nginx configuration, avoid codeigniter rewrite rules

This is nginx rewrite rule for codeigniter.
We can find this easily with googling.
server
{
server_name .example.com;
access_log /var/log/nginx/example.com.access.log;
root /var/www/example.com/html;
index index.php index.html index.htm;
# enforce www (exclude certain subdomains)
# if ($host !~* ^(www|subdomain))
# {
# rewrite ^/(.*)$ $scheme://www.$host/$1 permanent;
# }
# enforce NO www
if ($host ~* ^www\.(.*))
{
set $host_without_www $1;
rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
}
# canonicalize codeigniter url end points
# if your default controller is something other than "welcome" you should change the following
if ($request_uri ~* ^(/welcome(/index)?|/index(.php)?)/?$)
{
rewrite ^(.*)$ / permanent;
}
# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
rewrite ^/(.*)/index/?$ /$1 permanent;
}
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/(.+)/$ /$1 permanent;
}
# removes access to "system" folder, also allows a "System.php" controller
if ($request_uri ~* ^/system)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# catch all
error_page 404 /index.php;
# use fastcgi for all php files
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/example.com/html$fastcgi_script_name;
include fastcgi_params;
}
# deny access to apache .htaccess files
location ~ /\.ht
{
deny all;
}
}
I want to add normal php project in this server.
The project's root directory is /var/www/another/proj_new.
As I mentioned, this new project do not use codeigniter frame.
This is normal php file project. So it doesn't need Codeigniter rewrite rule.
So, my question is is possible that I can access the new project through the web.
the address may be like this:
http://example.com/proj_new
This address should not call codeigniter's proj_new controller.
I've tried to add this setting :
server
{
....
....
....
localhost /proj_new {
root /var/www/another/proj_new
index index.php
}
....
....
....
}
but, http://example.com/proj_new
makes 404 error pages.
I suggest this configuration from Nginx
server {
server_name nginxcodeigniter.net;
root /var/www/codeigniter;
index index.html index.php;
# set expiration of assets to MAX for caching
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location / {
# Check if a file exists, or route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
After this, make sure that your codeIgniter config.php contains the following information:
$config['base_url'] = "http://nginxcodeigniter.net/";
$config['index_page'] = "";
$config['uri_protocol'] = "REQUEST_URI";
Source: Nginx
The !-e in the following section of code means that if the file, directory or symlink does not exist, redirect to use the rewrite rule. The fact that you have this present should be enough for you to just create a folder proj_new and the rewrite rule should be ignored.
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
I presume you have tried just creating the proj_new folder already? It looks to me as if you already have sufficient means to achieve what you want in your file and I can't see any errors with it. You are creating your proj_new folder inside the html folder, right?
Just had a play about with this and it works fine. Your configuration works as expected. Attached below is my nginx.conf file so you can have a look. This was CI2.1, Nginx 1.0.1 Stable, Windows 7, PHP 5.3.1.
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
index index.php index.html index.htm;
# enforce NO www
if ($host ~* ^www\.(.*))
{
set $host_without_www $1;
rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
}
# canonicalize codeigniter url end points
# if your default controller is something other than "welcome" you should change the following
if ($request_uri ~* ^(/welcome(/index)?|/index(.php)?)/?$)
{
rewrite ^(.*)$ / permanent;
}
# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
rewrite ^/(.*)/index/?$ /$1 permanent;
}
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/(.+)/$ /$1 permanent;
}
# removes access to "system" folder, also allows a "System.php" controller
if ($request_uri ~* ^/system)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# catch all
error_page 404 /index.php;
# use fastcgi for all php files
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to apache .htaccess files
location ~ /\.ht
{
deny all;
}
}
}
What version of nginx are you using? This should work on newer versions, with the try_files directive.
http://ericlbarnes.com/post/12197460552/codeigniter-nginx-virtual-host
server {
server_name .mysecondsite.com;
root /sites/secondpath/www;
index index.html index.php index.htm;
# set expiration of assets to MAX for caching
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location / {
# Check if a file exists, or route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
s// try this change the "backend" with your projectname
location / {
# Check if a file or directory index file exists, else route it to index.php.
try_files $uri $uri/ /index.php;
}
# canonicalize url end points
# if your default controller is something other than "welcome" you should change the following
if ($request_uri ~* ^(/welcome(/index)?|/index(.php)?)/?$)
{
rewrite ^/backend/(.*)$ /backend/ permanent;
}
# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
rewrite ^/backend/(.*)/index/?$ /backend/$1 permanent;
}
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/backend/(.+)/$ /backend/$1 permanent;
}
# removes access to "system" folder
if ($request_uri ~* ^/system)
{
rewrite ^/backend/(.*)$ /backend/index.php?/$1 last;
break;
}
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/backend/(.*)$ /backend/index.php?/$1 last;
break;
}
can you add:
Can access to "backend" folder
if ($request_uri ~* ^/backend)
{
rewrite ^/(.*)$ /backend/index.php?/$1 last;
break;
}

Nginx: Rewrite /sitemap.xml to /sitemap

I have a Codeigniter PHP app, and I have configured my nginx conf to pretty up some of the URLs. For example, /index.php/home will rewrite to /home.
Now I am trying to configure nginx to rewrite http://mysite.com/sitemap.xml to /sitemap (which constructs the xml file). This is what I am using in my nginx conf:
if ($request_uri ~* ^/sitemap.xml)
{
rewrite ^/(.*)$ /index.php/sitemap last;
}
For some reason, the above doesn't work, I think another rule might be conflicting, but I can't figure out a workaround. Can anyone help? (full config file is below)
server {
listen 80;
root /www/myapp;
index index.php index.html index.htm;
access_log /var/log/nginx/access.log;
if (-f /www/myapp/application/errors/error_503.html)
{
return 503;
}
# canonicalize codeigniter url end points
#/home will redirect to /
if ($request_uri ~* ^(/home(/index)?|/index(.php)?)/?$)
{
rewrite ^(.*)$ / permanent;
}
# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
rewrite ^/(.*)/index/?$ /$1 permanent;
}
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/(.+)/$ /$1 permanent;
}
# removes access to "system" folder, also allows a "System.php" controller
if ($request_uri ~* ^/system)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
############THIS PART IS NOT WORKING
if ($request_uri ~* ^/sitemap.xml)
{
rewrite ^/(.*)$ /index.php/sitemap last;
}
####################################
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
## Default location
location / {
root /www/myapp;
index index.html index.htm index.php;
}
## Images and static content is treated different
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
access_log off;
expires 30d;
root /www/myapp;
}
## Parse all .php file in the www directory
location ~ .php$ {
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME /www/myapp/$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}
## catch all
#error_page 404 /index.php;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
## Disable viewing .htaccess & .htpassword
location ~ /\.ht {
deny all;
}
}
Problem above is that REQUEST_URI remains as /sitemap.xml instead of /sitemap, so CI can't route it properly. Try changing "last" to "redirect":
if ($request_uri ~* ^/sitemap.xml)
{
rewrite ^/(.*)$ /sitemap redirect;
}
Also consider using LOCATION instead of IF (avoid IF in general):
location = /sitemap.xml
{
rewrite .* /sitemap redirect;
}
Hope that helps
I use it in on my website:
# Sitemap.xml
location /sitemap.xml {
alias /venvs/mobilepro/src/mobilepro/static/maps/map.xml;
}

Resources