htacces for nginx

0
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1

Options -Indexes

I need to convert this rule to htaccess to nginx

    
asked by anonymous 13.09.2016 / 20:33

1 answer

1

In nginx use the try_files and instead of the variable $_GET['url'] use REQUEST_URI

nginx.conf should look something like:

location / {
    autoindex on;
    index  index.html index.htm index.php;
    try_files $uri $uri/ /index.php?$query_string;
}

An example to get the PATH (based on a framework I did link ):

function UrlPath()
{
    static $pathInfo;

    if ($pathInfo !== null) {
        return $pathInfo;
    }

    $sname  = $_SERVER['SCRIPT_NAME'];
    $reqUri = empty($_SERVER['REQUEST_URI']) ? null :
                preg_replace('#\?(.*)$#', '', $_SERVER['REQUEST_URI']);

    $pathInfo = rtrim(strtr(dirname($sname), '\', '/'), '/');

    $pathInfo = substr(urldecode($reqUri), strlen($pathInfo) + 1);

    $pathInfo = '/' . ($pathInfo === false ? '' : $pathInfo);

    return $pathInfo;
}

To use just call it like this:

echo UrlPath();

Instead of:

echo $_GET['url'];

This way with the UrlPath function will not affect the GET variables, if you need to try_files to apache do so:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^ index.php [L]
    
13.09.2016 / 21:53