简体   繁体   中英

converting apache htaccess to nginx

I am trying to convert apache htaccess rule to nginx config but it is not working. Following are the details of rules and nginx config:

.htaccess Rule

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php/$1 [L]

Nginx Config For Above

server {

    listen      ip_address:80;

    server_name  www.example.com;

    access_log /var/log/nginx/example.com-access.log;

    error_log /var/log/nginx/example.com-error.log;

    root   /var/www/html/example.com/;

    index  index.php;

location / {

                if (!-e $request_filename){

                rewrite ^(.*)$ /index.php/$1 break;

}

}

when i access website it opens home page and when i click other menus it gives 404 not found.

Following is the error log:

2015/03/18 05:31:56 [error] 3550#0: *7 open() "/var/www/html/example/index.php//contents.html" failed (20: Not a directory), client:

1.2.3.4, server: www.example.com, request: "GET /contents.html HTTP/1.1", host: "www.example.com", referrer: "http://example.com/"

any ideas???

Shoaib..

Your nginx rewrite is wrong – because of the slash in the end, nginx thinks index.php is a folder. You could do something like:

location / {
            if (!-e $request_filename) {
              rewrite ^(.*)$ /index.php?url=$1 break;
            }
}

And then in your PHP code parse $_REQUEST['url']

Another possibility, many webmasters are doing it this way:

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

Meaning – try to open a file first (if it exists), try to open a folder (if it exists), last resort – pass it to index.php. $args will contain query string, you can access original URL through $_SERVER.

Issue has been resolved, we enabled following in website's config.php file,

$config['index_page'] = ''; $config['uri_protocol'] = 'REQUEST_URI';

And nginx config as follows,

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

it worked...

Thanks @Denis and @SuddenHead for your replies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM