将.htaccess转换为Nginx - 当前的re_rewrite不起作用

时间:2012-12-26 15:58:17

标签: .htaccess mod-rewrite nginx

作为Nginx的新手(我真的是一个新手)我发现我的.htaccess网址重写不适用于Nginx。

我在这里找到了一些转换器: http://winginx.com/htaccess

我尝试在那里转换我的.htaccess,但是在集成之后我发现了一个奇怪的结果,一些url在点击它们时提示下载等等。

也许我误解了如何整合它,但我所做的是在.conf中为我的特定网站定义它,即:

/etc/nginx/sites-available/mydomain.conf

我原来的.htaccess看起来如下:

#php_value arg_separator.output &

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\.mydomain.com\.com$
RewriteRule (.*) http://www.mydomain.com/$1 [R=301,L]

RewriteRule ^([^/\.]+)/?$ index.php?cat=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ picture-normal.php?pic=$2 [L]
#RewriteRule ^([^/\.]+)/([^/\.]+)/(.*)$ ./$3 [L]

#RewriteRule ^[^/]/([0-9]*) test.html?id=$1

#RewriteRule ^set([0-9]*)-Assorted_Images_([0-9\-]*).html set.php?id=$1
#RewriteRule ^Funny-(.*)-([0-9]*).html jokes.php?id=$2
#RewriteRule ^Funny-(.*)-([0-9]*)-page([0-9]*).html jokes.php?id=$2&page=$3
#RewriteRule ^funny-joke-([0-9]*)-(.*).html joke.php?id=$1

#RewriteRule ^(.*).html index.php?p=$1

转换为:

    # nginx configuration

    location / {
      if ($http_host !~ "^www\.mydomain\.com$"){
        rewrite ^(.*)$ http://www.mydomain.com/$1 redirect;
      }
      rewrite ^/([^/\.]+)/?$ /index.php?cat=$1 break;
      rewrite ^/([^/\.]+)/([^/\.]+)/?$ /picture-normal.php?pic=$2 break;
    }

整个.conf看起来像这样:/etc/nginx/sites-available/mydomain.conf:

server
{
    server_name .mydomain.com;

    access_log /var/log/nginx/mydomain.com.access.log;

        error_log /var/log/nginx/mydomain.com.error.log;

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

    index index.php index.html index.htm;

    # use fastcgi for all php files
    location ~ \.php$
    {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # deny access to apache .htaccess files
    location ~ /\.ht
    {
        deny all;
    }

    # nginx configuration

    location / {
      if ($http_host !~ "^www\.mydomain\.com$"){
        rewrite ^(.*)$ http://www.mydomain.com/$1 redirect;
      }
      rewrite ^/([^/\.]+)/?$ /index.php?cat=$1 break;
      rewrite ^/([^/\.]+)/([^/\.]+)/?$ /picture-normal.php?pic=$2 break;
    }
}

1 个答案:

答案 0 :(得分:0)

如果您确实正确设置了PHP fastcgi,这应该适合您。

server {
    listen 80;
    server_name mydomain.com;
    return       301 http://www.mydomain.com$request_uri;
}
server {
    listen 80;
    server_name www.mydomain.com;
    access_log /var/log/nginx/mydomain.com.access.log;
    error_log /var/log/nginx/mydomain.com.error.log;
    root /var/www/mydomain.com/html;
    index index.php index.html index.htm;

    # use fastcgi for all php files
    # Are you sure you have this set up?
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # deny access to apache .htaccess files
    location ~ /\.ht {
        deny all;
    }

    # nginx configuration
    location / {
        rewrite ^([^/\.]+)/?$ index.php?cat=$1 last;
        rewrite ^([^/\.]+)/([^/\.]+)/?$ picture-normal.php?pic=$2 last;
    }
}

主要区别在于在单独的服务器块中执行mydomain到www.mydomain重定向更有效,并用“last”替换“break”,如果PHP fastcgi正确设置,将解决下载问题。< / p>