-1

Finally I decide to ask here after hours trying, searching and researching.

Months ago I migrated my site to a newer version and everything works fine with my old hosting in apache.

Recently I migrate to a VPS where I decide to use Nginx as server and I'm working in the details.

My problem is that I'm trying to make some specific redirections using a PHP script.

The redirections works pretty well in apache (locally and remotelly) but in nginx does not.

The strange behavior is that for example when I try to use the URL fakesite.tk/Section/index.php apache redirect to fakesite.tk/Section/ but Nginx returns a 404 error and if I try the URL fakesite.tk/Section/index.php/ mysteriously work (note the slash at the end) and redirect to fakesite.tk/Section// (Note the double slash)

I tryed to add a slash to the end of all URLS but this redirection equally don't work in Nginx.

If it matters, my VPS runing in Ubuntu (iquals that my host) and I make tests in a Windows machine.

There are my Nginx site config file:

server
{
         server_name *.fakesite.tk;
         return 301 $scheme://www.fakesite.tk$request_uri;
}

#Redirect non www to www site version
server
{
         server_name fakesite.tk;
         return 301 $scheme://www.fakesite.tk$request_uri;

}

server
{
        listen 80;
        listen [::]:80;

        root /var/www/SiteFolder;
        index index.php;

        server_name www.fakesite.tk;

        #Stabilishing error 404 and 403 error pages
        error_page 404 /?error=404;
        error_page 403 /?error=403;

        #Friendly URLs
        location /
        {
        location /
        {
                try_files $uri $uri/ =404;
                rewrite ^/([^/]*)/$ /?sect=$1 last;
                rewrite ^/([^/]*)/([^/]*)/$ /?sect=$1&lang=$2 last;
                rewrite ^/([^/]*)/([^/]*)/([^/]*)/$ /?sect=$1&lang=$2&cont=$3 last;
                rewrite ^/([^/]*)/([^/]*)/([^/]*)/([^/]*)/$ /?sect=$1&lang=$2&cont=$3&subcont=$4 last;
        }

        #Adding expire header
        location ~* \.(?:ico|css|js|gif|jpe?g|png|eot|svg|ttf|woff)$
        {
                expires 30d;
                add_header Pragma public;
                add_header Cache-Control "public";
        }

        #Enabling PHP
        location ~ \.php$
        {
                # With php5-fpm:
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
#                fastcgi_index index.php;
                include fastcgi_params;
        }

        #deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one

        location ~ /\.ht
        {
                deny all;
        }
}

And my php redirection script:

<?php 
    ini_set('display_errors', true);//Si estamos en local se muestran con normalidad los errores.
    error_reporting(E_ALL);
    $url = "http://$_SERVER[SERVER_NAME]$_SERVER[REQUEST_URI]";
    $newurl = $url;

    $newurl = str_replace('_', '-', $url);//Reeplace all _ with -


    $newurl = str_replace('index.php', '', $newurl);//Removing a index.php

    $newurl = str_replace('.php', '', $newurl);//Removing all .php

    $newurl = str_replace('/Intro', '', $newurl);//Removing all intro sections

    $extens = preg_match('/\.(jpg|gif|png|jpeg|js|css|woff|html|eot|svg|ttf|xml|map|min|txt)/', $newurl);

    if($extens !== 1 && $newurl[strlen($newurl) - 1] !== '/') //Trying to put a slash at the end
    {
        $newurl.='/';
    }

    if($url !== $newurl)
    {
        header("HTTP/1.1 301 Moved Permanently");
        header("Location: $newurl");
    }
?>

I suppose that maybe is very small detail that I even not see, so I appreciate your help. Many thanks.

EDIT: I verified that the var $newurl effectively changed and goes inside the if condition, but the header(....); lines does not executed, I put an exit(); command after that and it is being executed, it means that affectively the header(...); lines not been executed.

EDIT 2: When I put manually a underscore in the URL and slash at the end of it, this redirects pretty good but if I don´t put a slash at the end it does not work even when run the conditional block.

2
  • Did you inspect the logs?
    – 030
    Sep 1, 2014 at 18:02
  • Of course @utrecht the logs look fine no error there. In fact I finally solved it, I will post the solution in few minutes.
    – JuanDMeGon
    Sep 1, 2014 at 18:09

1 Answer 1

0

Ok guys. I finally solved it, just like I said was a very small detail (the small details are the most difficult always).

The problem was with the fcgi for PHP configured in the virtual host file for nginx.

In the original file (below) I said to fcgi that try find the file and if not trigger a 404 error, this was my mistake because it trigger his self error and don't allow to my site to manage this, so the redirection script does not be executed.

#Enabling PHP
location ~ \.php$
{
    # With php5-fpm:
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

To solve the problem. I simply said to the fcgi that send the code error like a parameter to allow to the site manage it, just like this:

#Enabling PHP
location ~ \.php$
{
    # With php5-fpm:
    #try_files $uri =404; #Mistake here!!
    try_files $uri /?error=404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

EDIT: I prefer to add the trailing slash at the end of the URL (to avoid 404 errors) using the proper nginx virtual host config file because PHP doesn't work properly. I only added the following line inside the server block: #Adding trailing slash at the end rewrite ^([^.]*[^/])$ $1/ permanent;

Notice that it only redirects when the URL have not a dot '.'.

I hope that someone could use this experience to solve a related problem, in the future.

See you then.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .