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 /
Related
I've just deployed a newly built website running Laravel 5 and nginx and struggling to setup my nginx config to 301 redirect some old URLs - specifically those with .php extensions.
I'm looking to 301 redirect the below...
domain.com/foobar.php?id=123 -> domain.com/foobar/123
The default config assumes everything runs from index.php, so Laravel does not trigger on foobar.php.
I have tried adding the following rewrite rule, but it doesn't trigger (I assume because nginx isn't listening out for foobar.php requests?
rewrite ^/foobar.php(.*)$ /foobar/$1/ permanent;
Relevant snippet of my current nginx config below...
server {
listen 443 ssl;
server_name domain.com;
root /home/domain.com/public;
index index.html index.htm index.php;
charset utf-8;
if ($request_method = GET ) {
rewrite ^([^.]*[^/])$ $1/ permanent;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/domain.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
This should work:
rewrite ^/foobar.php /foobar/$arg_id/? permanent;
You can get any argument like this $arg_{your_argument}. Just replace {your_argument} with the actual argument name.
I don't get it why the mvc routes are not working.
When I access the home page, all the css and js are loaded.
"GET /css/main.css HTTP/1.1" 304
But when I access any other controller, I got:
method=GET path="/transaction/add"
[error] open() "/app/public/transaction/add" failed (2: No such file or directory)
"GET /transaction/add HTTP/1.1" 404
Here's my procfile
--Procfile--
web: vendor/bin/heroku-php-nginx public/
location / {
# try to serve file directly, fallback to rewrite
try_files $uri #rewriteapp;
}
location #rewriteapp {
# rewrite all to app.php
rewrite ^(.*)$ /index.php/$1 last;
}
location ~ ^/(app|app_dev|config)\.php(/|$) {
try_files #heroku-fcgi #heroku-fcgi;
internal;
}
I'm really lost here.
This should work:
location / {
# try to serve file directly, fallback to rewrite
try_files $uri #rewriteapp;
}
location #rewriteapp {
# rewrite all to app.php
rewrite ^(.*)$ /index.php/$1 last;
}
location ~ ^/index\.php(/|$) {
try_files #heroku-fcgi #heroku-fcgi;
internal;
}
Try to follow the instructions in the documentation: http://docs.phalconphp.com/fr/latest/reference/nginx.html
server {
listen 80;
server_name localhost.dev;
index index.php index.html index.htm;
set $root_path '/var/www/phalcon/public';
root $root_path;
try_files $uri $uri/ #rewrite;
location #rewrite {
rewrite ^/(.*)$ /index.php?_url=/$1;
}
location ~ \.php {
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index /index.php;
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
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;
}
}
I am running magento under the Wordpress(Wordpress on the root and mangento under a subdirectory "/shop"). Previously I was running this under the Apache, So everything was running fine with separate .htaccess under the /shop folder.
As I move the server over NginX, all the internal magento URL showing 404. Please help me to solve this problem. Please suggest me how can I run the Magento under the /shop folder with Wordpress.
Here is my NginX Configuration file:
server {
listen 80;
server_name domain.com www.domain.com;
root /var/www/html;
index index.php index.html index.htm;
error_log /var/www/logs/error.log;
access_log /var/www/logs/access.log;
### gZip Setting ###
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
### gZip End ###
location / {
if ($http_host !~ "^www\."){
rewrite ^(.*)$ http://www.$http_host/$1 redirect;
}
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location /shop/ {
try_files $uri $uri/ /index.php?$args;
}
location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
rewrite ^(.*.php)/ $1 last;
}
if (!-e $request_filename)
{
rewrite ^(.+)$ /index.php?q=$1 last;
}
location ~ .php$ { ## Execute PHP scripts
if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
expires off; ## Do not cache dynamic content
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params; ## See /etc/nginx/fastcgi_params
}
}
I got the solution, Here is my new configuration file:
server {
listen 80;
server_name domain.com www.domain.com;
root /var/www/html;
index index.php index.html index.htm;
error_log /var/www/logs/domain-error-ssl.log;
access_log /var/www/logs/domain-access-ssl.log;
### gZip Setting ###
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
### gZip End ###
location / {
if ($http_host !~ "^www\."){
rewrite ^(.*)$ https://www.$http_host/$1 redirect;
}
try_files $uri $uri/ /index.php?$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
rewrite ^(.*.php)/ $1 last;
}
### Configuration for shop ###
location /shop {
index index.html index.php;
try_files $uri $uri/ #handler;
expires 30d;
if ($uri ~ "^/index.php/admin.*$"){
rewrite ^/index.php/admin(.*) /admin$1 redirect;
}
}
location ~ ^/shop/(app|includes|lib|media/downloadable|pkginfo|report/config.xml|var)/ { internal; }
location /shop/var/export/ { internal; }
location #handler { rewrite / /shop/index.php; }
### END Configuration for shop ###
location ~ .php$ { ## Execute PHP scripts
expires off; ## Do not cache dynamic content
fastcgi_read_timeout 120;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
#fastcgi_param HTTPS $fastcgi_https;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params; ## See /etc/nginx/fastcgi_params
}
}
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;
}
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;
}