htacces for Nginx

Asked

Viewed 151 times

0

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

Options -Indexes

I need to convert that rule into htaccess for nginx

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 https://github.com/inphinit/framework/blob/master/src/Utils.php#L62):

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 so:

echo UrlPath();

Instead of:

echo $_GET['url'];

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

RewriteEngine On

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

RewriteRule ^ index.php [L]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.