What is nginx equalant for my htaccess mod_rewrite?

15,176

Since does not have an equivalent to the .htaccess file (i.e. no directory level configuration files), you need to update the main configuration and reload nginx for any changes to take effect.

The above Apache config essentially reads "if the path provided isn't an existing file or a directory, redirect to index.php, appending the path".

In Nginx, you will use the try_files directive to accomplish the same thing:

location / {
        try_files $uri $uri/ @ci_index;
}

location @ci_index{
        rewrite ^(.*) /index.php?$1 last;
}

Unlike with Apache - if statements are best avoided in Nginx configs.

In some setups, you may be able to avoid the named index and rewrite, and simply use /index.php as the 3rd path to try_files (and codeigniter should get the path from $_SERVER[$config['uri_protocol']].

As for the use of PATH_INFO - check your fastcgi_params file (which you hopefully included in your php location block) and look:

fastcgi_param  PATH_INFO          $fastcgi_path_info;

You may also be able to use $config['uri_protocol'] = "REQUEST_URI"

For any option you choose, check the output of print_r($_SERVER) to verify which server variables have been set, and what they have been set to (they should match up with whatever you have specified in your PHP location block and fastcgi_params file).

Share:
15,176

Related videos on Youtube

Arun David
Author by

Arun David

Updated on September 18, 2022

Comments

  • Arun David
    Arun David over 1 year

    Previously i was using linode VPS running Apache for my CodeIgniter website. Today i installed nginx and my website landing page is coming but the other pages that is using htaccess for rewriting URL is not coming. This is my htaccess,

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

    What is the nginx equalent for my htaccess? Also is there any changes do I need to make in my codeigniter config and application??..

    For URI protocal in codeigniter config, I am using,

    $config['uri_protocol'] = 'PATH_INFO';
    

    Will this work with nginx??..

  • Arun David
    Arun David about 12 years
    Thnx friend. Got it working :) Although PATH_INFO is not working, AUTO is working same like PATH_INFO.