0

I need to rewrite (convert) this basic htaccess rules to nginx format. Can you help me? Thanks.

RewriteEngine On  

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

RewriteRule ^(.*)$ ./index.php?request=$1

And this is my whole site.com nginx config;

server {
listen 80;
#listen [::]:80 default ipv6only=on;
server_name www.domain.com domain.com;
root /home/user/domains/domain.com/public_html;
access_log /home/user/domains/domain.com/logs/access.log;
error_log /home/user/domains/domain.com/logs/error.log;

index index.php index.html index.htm;
error_page 404 /404.html;

location / {
   try_files $uri $uri/ /index.php?request=$uri;
}

location ~ \.php$ {
    try_files $uri =403;
    fastcgi_pass unix:/var/run/php5-fpm-user.sock;
    include fastcgi_params;
}

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

And this is index.php file contents; if its going to help. it has the clean url functions in it;

<?php
header('Content-type: text/html; charset=utf-8');

if ( !isset($_GET['debug']) )
    error_reporting(0);

if ( !defined("HQ") || !defined("HQ") )
{
    echo "No sneaky!";
    exit;
}

define( "SYSTEM", HQ . "system/" );
define( "SYSTEM_URL", HQ_URL . "system/" );
define( "FRONTEND", "true" );

// Required files
require_once SYSTEM . "assistants/utf8.php";
require_once SYSTEM . "assistants/config.inc.php";
require_once SYSTEM . "assistants/clerk.php";
require_once "display_functions.php";

// Initialize
$clerk= new Clerk();
$clerk->dbConnect();
$clerk->loadSettings();

// Load plugins
loadPlugins();

// Include all modules
$defaultModules= array( "global", "design", "pages" );
$modules= scanFolder( SYSTEM . "modules", 1 );

foreach ( $defaultModules as $key )
{
    $viewFile= SYSTEM . "modules/" . $key . "/view.php";
    if ( file_exists( $viewFile ) )
    {
        include_once $viewFile;
    }
}

foreach ( $modules as $key => $val )
{
    $viewFile= SYSTEM . "modules/" . $key . "/view.php";
    if ( file_exists( $viewFile ) )
    {
        include_once $viewFile;
    }
}

// Clean URL switching, if requested
// for debugging purposes
if ( isset( $_GET['clean_urls'] ) )
{
    if ( $_GET['clean_urls'] == 0 )
    {
        $clerk->updateSetting( "clean_urls", array( 0 ) );
    }
    elseif ( $_GET['clean_urls'] == 1 )
    {
        $clerk->updateSetting( "clean_urls", array( 1 ) );
    }
}

// URI Router
if ( $clerk->getSetting( "clean_urls", 1 ) == 1 && empty( $_GET['request'] ) == false )
{
    $_GET['request']= utf8_strtolower( $_GET['request'] );

    $uri_routes= call_anchor( "uri_router", array() );

    foreach ( $uri_routes as $pattern => $result )
    {
        if ( preg_match( "#$pattern#", $_GET['request'] ) )
        {   
            $remainder= preg_replace( "#$pattern#", "", $_GET['request'], 1 );

            if ( !empty( $remainder ) ) continue;

            $match= preg_replace( "#$pattern#", $result, $_GET['request'], 1 );
            parse_str( $match, $map );
        }
    }

    $_GET= $map;
}

foreach ( $modules as $key => $val )
{
    $module= getRemappedVar( $key );

    if ( in_array( $key, $defaultModules ) )
    {
        if ( $module == getRemappedVar("pages") && !empty( $_GET['id'] ) )
        {   
            $pageDetails= pageInfo( $_GET[getRemappedVar("pages")] );
            $pageType= $pageDetails['content_type'];

            $layout= $pageType;
            $activeModule= $pageType;
        }

        continue;
    }

    if ( array_key_exists( $module, $_GET ) )
    {
        $layout= $key;
        $activeModule= $key;

        break;
    }
}

if ( empty( $layout ) || $layout == "pages" ) $layout= "default";

$index_page= pageInfo( $clerk->getSetting( "index_page", 1 ) );
$index_page= $index_page['slug'];

$selectedPage= ( empty( $_GET[getRemappedVar("pages")] ) ) ? $index_page : $_GET[getRemappedVar("pages")];
foreach ( $modules as $key => $val )
{
    $module= getRemappedVar( $key );

    if ( !empty( $_GET[getRemappedVar($key)] ) && $module == getRemappedVar("pages") )
    {
        $selectedPage= $_GET[getRemappedVar($key)];
        break;
    }

    // Not viewing a page (domain.com/?projects=id)
    elseif ( !empty( $_GET[getRemappedVar($key)] ) && $module != getRemappedVar("pages") )
    {
        $selectedPage= getRemappedVar($key);
        break;
    }
}

// Constants
call_anchor( "site_init" );

define( "THEME", $clerk->getSetting( "site_theme", 1 ) );
define( "THEME_URL", HQ_URL . "site/themes/" . THEME . "/" );
define( "LAYOUT", "layout_" . $layout . ".php" );
define( "ACTIVE_MODULE", $activeModule );
define( "PAGE", $selectedPage );

// Check if layout exists.
if ( file_exists( HQ . "site/themes/" . THEME . "/" . LAYOUT ) == false )
{
    echo "Oops! Looks like your theme is missing the layout file, <em>" . LAYOUT . "</em>.<br /><br />Create this file and upload it to the root of your theme's folder. Don't forget to fill it with template tags and your custom HTML!";

    exit;
}

call_anchor( "site_begin" );

require_once "themes/" . THEME . "/" . LAYOUT;
?>
0

You should use try_files

location / {
    index index.php index.html index.htm;
    try_files $uri $uri/ /index.php?request=$args;
}
2
  • Thank you Mike but it didnt work. It didnt give any error but its not working. For example with out clean urls the url is like that; domain.com/?page=works after i enable clean urls it makes url like this domain.com/works but it doesnt load the page. Any idea? – borannb Jan 28 '13 at 17:24
  • What i did and achieved something. I changed the code like that; location / { try_files $uri $uri/ /index.php?page=$uri&$args; } And this is now works but its not working properly. Now all the links domain.com/projects, domain.com/blog and domain.com/about points to domain.com/?page=projects – borannb Jan 28 '13 at 18:21
0

As stated before, you should use try_files, though it needs to be rewritten a bit differently:

location / {
    try_files $uri $uri/ /index.php?request=$uri;
}
6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.